简体   繁体   English

如何在另一个数组中拼接某个项目

[英]How to splice a certain item within another array

I receive something like this:我收到这样的东西:

serverDocument.scheduledLines [ serverDocument.scheduledLines [

{ _id: 'd375d9f136d9aed8161e874b', resourceId: '54cc874716368a9be973d0cb', workOrderId: '53yc4SeHRErSaWZcr', { _id:'d375d9f136d9aed8161e874b',resourceId:'54cc874716368a9be973d0cb',workOrderId:'53yc4SeHRErSaWZcr',

}, { _id: '4136758377e2a0b89fcd15fb', resourceId: '54cc874716368a9be973d0cb', workOrderId: 't3cdTGDpxhm6sue5Z', },{_id:'4136758377e2a0b89fcd15fb',resourceId:'54cc874716368a9be973d0cb',workOrderId:'t3cdTGDpxhm6sue5Z',

}, { _id: '691cda0327a2b947bc40c008', resourceId: '54cc874716368a9be973d0cb', workOrderId: 'mzP5asqiMEKcYpPW9', }, { _id: '691cda0327a2b947bc40c008', resourceId: '54cc874716368a9be973d0cb', workOrderId: 'mzP5asqiMEKcYpPW9',

},] },]

I try to remove every resourceId element with serverDocument.scheduledLines.slice(0, 1), but in stead of removing the resourceId's, the second element is removed.我尝试使用 serverDocument.scheduledLines.slice(0, 1) 删除每个resourceId元素,但不是删除 resourceId,而是删除了第二个元素。 Is it possible to remove the resourceId in one level deeper?是否可以更深一层地删除 resourceId?

You can just remove the property from each array item with a map function您可以使用map function 从每个数组项中删除属性

 const array = [ { _id: 'd375d9f136d9aed8161e874b', resourceId: '54cc874716368a9be973d0cb', workOrderId: '53yc4SeHRErSaWZcr',}, { _id: '4136758377e2a0b89fcd15fb', resourceId: '54cc874716368a9be973d0cb', workOrderId: 't3cdTGDpxhm6sue5Z',}, { _id: '691cda0327a2b947bc40c008', resourceId: '54cc874716368a9be973d0cb', workOrderId: 'mzP5asqiMEKcYpPW9',}, ] // removing the resourceId array.map((item) => { delete item.resourceId; return item; }); console.log(array);

Thats because in your attempt you don't manipulate the items in the array, you just manipulate the array itself.那是因为在您的尝试中,您不会操纵数组中的项目,而只是操纵数组本身。

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

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