let list = [
{ id: 1, name: "zhangsan" },
{ id: 2, name: "lisi" },
{ id: 3, name: "wangwu" }
];
let newItem = { id: 2, name: "xiaoming", sex: "female" };
list = list.map(t => { return t.id === newItem.id
? newItem
: t;
});
console.log(list);
//[{"id":1,"name":"zhangsan"},{"id":2,"name":"xiaoming","sex":"female"},{"id":3,"name":"wangwu"}]
js将数组中的某一条替换为另一条数据
最新推荐文章于 2024-09-19 17:41:49 发布