简体   繁体   English

循环拼接以更新新的对象数组

[英]splice in a loop to update new array of objects

let index = findIndex(existingObjs, { '_id': data.id });
if (index > -1) {
    existingObjs.splice( index, 1, newObj );
}

Above code is working to replace updated obj after a PUT. 上面的代码可用于在PUT之后替换更新的obj。 But I have problem doing so if the newObj is an array and has multiple object. 但是,如果newObj是一个数组并且有多个对象,我这样做会遇到问题。

From what it seems you are trying to remove one and add multiple objects from the obj array newObj , you can do that using the spread operator . 从看来您正在尝试从obj数组newObj remove one对象并添加多个对象的newObj ,可以使用spread operator执行此spread operator

Do it in the following manner 按以下方式进行

let index = findIndex(existingObjs, { '_id': data.id });
if (index > -1) {
    existingObjs.splice( index, 1, ...newObj );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM