Javascript 使用新值更新现有JS关联数组

Javascript 使用新值更新现有JS关联数组,javascript,arrays,Javascript,Arrays,我想在下面的数组中搜索3.30pm,并在JS中更新数组索引的类型值 0: {time: "2:00pm",type:16} 1: {time: "3:30pm",type:30} 2: {time: "5:00pm",type:90} 任何人请建议正确的方法来解决它 我试过这个 function in_array(array, id) { console.log(array); for(var i=0;i<array.length;i++) { retu

我想在下面的数组中搜索3.30pm,并在JS中更新数组索引的类型

0: {time: "2:00pm",type:16}
1: {time: "3:30pm",type:30}
2: {time: "5:00pm",type:90}
任何人请建议正确的方法来解决它

我试过这个

 function in_array(array, id) {
    console.log(array);
    for(var i=0;i<array.length;i++) {
        return (array[i][0].time === id)
    }
    return false;
}
$result = in_array(timesShow, $time);
使用

试试这个

for(var i=0;i<array.length;i++) {
   if(array[i].time === id)
   {
       array[i].type='whatever you want to change';
   }
}
for(var i=0;i
var obj=[
{时间:“下午2:00”,类型:16},
{时间:“下午3:30”,键入:30},
{时间:“下午5:00”,键入:90}
];
obj.forEach(功能(ob){
如果(ob.time==“3:30pm”)ob.type=50;
});

console.log(obj)
您可以只使用该项,而不使用其他索引,并返回该项(如果找到)

函数getItem(数组,id){ var i; 对于(i=0;i
试试这个:

    var obj = [
     {time: "2:00pm",type:16},
     {time: "3:30pm",type:30},
     {time: "5:00pm",type:90}
    ];
    var finalResult = obj.filter(function(d){
     if(d.time == "3:30pm"){d['type'] = 40}
     return d;
    });
console.log(finalResult)

删除
[0]
。错误是告诉您
数组[i][0]
未定义的
。此外,您的
for
循环最多运行一次,因为您无条件返回。请将完整的JSON对象放入问题中,这将有助于给出准确的答案。您混合了php和javascript语法!javascript没有关联数组,in_数组不是内置javascript函数。
for(var i=0;i<array.length;i++) {
   if(array[i].time === id)
   {
       array[i].type='whatever you want to change';
   }
}
    var obj = [
     {time: "2:00pm",type:16},
     {time: "3:30pm",type:30},
     {time: "5:00pm",type:90}
    ];
    var finalResult = obj.filter(function(d){
     if(d.time == "3:30pm"){d['type'] = 40}
     return d;
    });
console.log(finalResult)