js将数组中的某一条替换为另一条数据

  • Post author:
  • Post category:其他


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"}]



版权声明:本文为sunzhen15896原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。