我在 javascript 中有一个这样的对象数组:
array=[{label: 1, value:e}, {label:1, value: i}, {label: 2, value:l}]
我想在值为 l 时找到标签,所以我在写:
array.map((i) => i.find(item => item.value === 'l').label)
但是返回错误
i.find is not a function
我错过了什么?
最佳答案
我不明白你为什么要使用 map
- 根据你的问题,你只是想找到具有特定值的元素的标签,所以你只需要 find
。这很好用:
array.find(item => item.value === 'l').label
返回 2
。
您应该处理 find
返回 undefined
的情况,例如:
var found = array.find(item => item.value === 'l')
if(found){
var label = found.label;
// use label
}else{
// nothing found
}
关于javascript - 查找不是函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410527/