Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6映射在对象数组上抛出错误_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript ES6映射在对象数组上抛出错误

Javascript ES6映射在对象数组上抛出错误,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我的代码obj没有定义,我的对象数组如下 const normalizeEventTypes = nextProps.events.space_events.map(obj, i => obj.id ) 我遗漏了什么吗?您忘记使用(),请这样写: { "space_events": [{ "id": 1, "name": "Anniversaries" }, { "

我的代码obj没有定义,我的对象数组如下

const normalizeEventTypes = nextProps.events.space_events.map(obj, i => 
     obj.id
)
我遗漏了什么吗?

您忘记使用
()
,请这样写:

{
    "space_events": [{
            "id": 1,
            "name": "Anniversaries"
        },
        {
            "id": 2,
            "name": "Brand Rollout"
        }
    }]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) => 
     obj.id
)
a = b.map((i,j) => i)
原因

map
回调函数中使用obj和index这两个参数,因此需要使用
()
来包装参数,如下所示:

{
    "space_events": [{
            "id": 1,
            "name": "Anniversaries"
        },
        {
            "id": 2,
            "name": "Brand Rollout"
        }
    }]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) => 
     obj.id
)
a = b.map((i,j) => i)
当我们只想使用一个参数时,这些
()
是可选的,如下所示:

{
    "space_events": [{
            "id": 1,
            "name": "Anniversaries"
        },
        {
            "id": 2,
            "name": "Brand Rollout"
        }
    }]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) => 
     obj.id
)
a = b.map((i,j) => i)
地图的不同使用方式:

1.
a.map(i=>i+1)//不需要索引时

2.

a = b.map(i => i)
3.
a.map((i,j)=>i+j)//何时要使用项的索引

检查工作代码段:

let数据={
“空间活动”:[{
“id”:1,
“姓名”:“周年纪念日”
},
{
“id”:2,
“名称”:“品牌展示”
}
]
}
让result=data.space\u events.map((obj,i)=>obj.name);

console.log('result',result)控制台中有错误吗?注意下面的@MayankShukla。如果问题确实解决了,一定要接受它。