我知道以前也有人问过类似的问题,但这次有点不同。我有一个未命名对象数组,其中包含一个命名对象数组,我需要获取“name”为“string 1”的对象。这是一个示例数组。
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
Update:我应该早点说这一点,但一旦找到它,我想用编辑过的对象替换它。
查找数组元素:
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find(o => o.name === 'string 1');
console.log(obj);
替换数组元素:
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find((o, i) => {
if (o.name === 'string 1') {
arr[i] = { name: 'new string', value: 'this', other: 'that' };
return true; // stop searching
}
});
console.log(arr);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)