这是我的数组:
0: {value: "VALUE1", label: "VALUE1"}
1: {value: "VALUE2", label: "VALUE2"}
我想在此数组中搜索值truck
,如果发现我需要运行一个函数,否则需要另一个函数
答案 0 :(得分:0)
function find(value, array) {
for(data of array) {
if(data['value'] === value) {
return data
}
}
return null
}
const result = find('truck', [
{ value: "VALUE1", label: "VALUE1" },
{ value: "VALUE2", label: "VALUE2" },
{ value: "truck", label: "VALUE2" }
])
if (result) {
console.log('found a truck', { result })
} else {
console.log('no truck found')
}