【问题标题】:Uncaught TypeError: {(intermediate value)}.map is not a function [duplicate]未捕获的TypeError:{(中间值)}.map不是函数[重复]
【发布时间】:2020-01-05 21:31:09
【问题描述】:

这是我的代码:

function employee(rex1){
    const rex = {...rex1}.map((key , value)=>{
        return `${key} : ${value}`


    })
    return `your result is ${rex.join(',')}`
}
console.log(employee({name : 'ahmed' , age : 20})

会报如下错误:

Uncaught TypeError: {(intermediate value)}.map 不是函数

【问题讨论】:

  • 对象没有map 方法。数组做~Array.prototype.map()
  • 嗨,欢迎来到 SO。为了在这里做出好的回答,请务必包含有关您的问题的更多详细信息,准确说明您需要帮助的内容,并正确格式化您的代码。谢谢!

标签: javascript


【解决方案1】:

如果你想遍历一个对象中的键/值对,你可以使用Object.entries()将它们作为数组检索到表单中

[[key1, val1], [key2, val2], ... [keyn, valn]]

例如

function employee(rex1) {
  const rex = Object.entries(rex1).map(([key , value]) => `${key} : ${value}`)

  return `your result is ${rex.join(',')}`
}
console.info(employee({name : 'ahmed' , age : 20}))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2018-01-02
    • 2017-01-26
    • 1970-01-01
    • 2016-01-21
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多