1、对象替换
var arr1 = [ { id: 100, name: '小麦', value: "1" }, { id: 101, name: '小明', value: "2" }, { id: 102, name: '小白', value: "3" }, { id: 103, name: '小青', value: "4" }, { id: 104, name: '小龙', value: "5" }, ]; var arr2={id:102,name:'小龙虾',value:"100"}; arr1 = arr1.map(t => { return t.id === arr2.id ? arr2 : t; }); console.log("结果",arr1); //结果 [{id: 100, name: "小麦", value: "1"}, {id: 101, name: "小明", value: "2"}, {id: 102, name: "小龙虾", value: "100"}, {id: 103, name: "小青", value: "4"}, {id: 104, name: "小龙", value: "5"}]
2、数组替换
var arr1 = [ { id: 100, name: '小麦', value: "1" }, { id: 101, name: '小明', value: "2" }, { id: 102, name: '小白', value: "3" }, { id: 103, name: '小青', value: "4" }, { id: 104, name: '小龙', value: "5" }, ]; var arr3=[{id:102,name:'小龙虾',value:"100"},{id:104,name:'明道',value:"001"}]; this.arr1 = this.arr1.map(item => { const data = arr3.find(i => item.id == i.id); return { ...item, ...data }; }); console.log("结果",arr1); //结果 [{id: 100, name: "小麦", value: "1"}, {id: 101, name: "小明", value: "2"}, {id: 102, name: "小龙虾", value: "100"}, {id: 103, name: "小青", value: "4"}, {id: 104, name: "明道", value: "001"}]
标签: 数组替换
上面是“js把数组里数据用另一个数组的替换(通过相同”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_127.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!