Uncaught TypeError: {(中间值)}.map 不是一个函数"

huangapple go评论75阅读模式
英文:

Uncaught TypeError: {(intermediate value)}.map is not a function"

问题

const genreOptions = [
  { value: "Horror", label: "Korku" },
  { value: "Comedy", label: "Komedi Filmi" },
  { value: "Action", label: "Aksiyon" },
  { value: "Drama", label: "Drama" }
];
英文:

Actual code:

const genreOptions = [{{ genreOptions | json_encode | raw }}].map((type , label) => ({value: type, label: label}));

Debugging code:

            const genreOptions = {
                "Horror": "Korku",
                "Comedy": "Komedi\u0103 Filmi",
                "Action": "Aksiyon",
                "Drama": "Drama"
            }.map((type,label)=>({
                value: type,
                label: label
            }));

Im getting the following error from above code:

Uncaught TypeError: {(intermediate value)(intermediate value)(intermediate value)(intermediate value)}.map is not a function"

Array Dump from Backend Now:

array:4 [▼
  "Horror" => "Korku"
  "Comedy" => "Komedi\u0103 Filmi"
  "Action" => "Aksiyon"
  "Drama" => "Drama"
]

Array Dump Backend Before Modification:

array:4 [▼
  0 => "Horror"
  1 => "Comedy"
  2 => "Action"
  3 => "Drama"
]

We are trying now to translate the website and therefore I put the actual Words as keys in array instead of 0,1,2,3... and then the translations.

But we're getting ".map is not a function error [...]" .map() is used only on arrays and we're surprised why the new version isn't well received as array.

I added Square brackets [] and the error was gone but the result was not as we've had expected it.

I would like Comedy to be assigned to "value" and Korku to "label".

答案1

得分: 0

const genreOptions = Object.entries({
        "Horror": "Korku",
        "Comedy": "Komediă Filmi",
        "Action": "Aksiyon",
        "Drama": "Drama"
    }).map(
    ([type, label])=>({ 
        value: type,
        label: label
    })
)
英文:

genreOptions is an object with key/value, you could use Object.entries then map to get the expected output :

const genreOptions = Object.entries({
        "Horror": "Korku",
        "Comedy": "Komedi\u0103 Filmi",
        "Action": "Aksiyon",
        "Drama": "Drama"
    }).map(
    ([type, label])=>({ 
        value: type,
        label: label
    })
)

huangapple
  • 本文由 发表于 2023年7月27日 22:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定