简体   繁体   中英

JS intersection of array and object of arrays

I want to delete some item from my object :

clientData : {
1111 : [
  {ID : 112, name : 'John',age : 23},
  {ID : 113, name : 'Doe',age : 21},
  {ID : 114, name : 'Stan',age : 24},
],

2222 : [
  {ID : 222, name : 'Sara',age : 15},
  {ID : 223, name : 'Wiliams',age : 61},
  {ID : 224, name : 'Alan',age : 45},
],

}

The data that I want to delete is stored in an array of Id's like this :

[112,223,114]

So the output should look like this :

clientData : {
1111 : [
  {ID : 113, name : 'Doe',age : 21},
],
2222 : [
  {ID : 222, name : 'Sara',age : 15},
  {ID : 224, name : 'Alan',age : 45},
],
}

Any help please how to achieve this ? I really have no idea how to do it, and don't found any solutions in internet.

Loop through each object property and filter the array based on the delete ID's.

let deleteIds = [112,223,114];

for (const data in clientData) {
  clientData[data] = clientData[data].filter(list=>!deleteIds.includes(list.ID));
}

console.log(clientData) // prints out your object

I am assuming you are tying to filter out the original object. Hence, You can simply achieve it by using Array.forEach() method along with the Array.splice() .

Live Demo :

 const clientData = { 1111 : [ {ID : 112, name : 'John',age : 23}, {ID : 113, name : 'Doe',age : 21}, {ID : 114, name : 'Stan',age : 24}, ], 2222 : [ {ID : 222, name : 'Sara',age : 15}, {ID : 223, name : 'Wiliams',age : 61}, {ID : 224, name : 'Alan',age : 45}, ] }; const invalidValues = [112, 223, 114]; Object.keys(clientData).forEach(key => { clientData[key].forEach((obj, index) => { if (invalidValues.includes(obj.ID)) { clientData[key].splice(index, 1); } }) }); console.log(clientData);

You could use Array.reduce() along with Array.filter() to delete the items from the client array.

I would suggest creating a Set to contain the ids to be deleted. This will make lookup more efficient than using an array (using Set.has )

 const clientData = { 1111 : [ {ID : 112, name : 'John',age : 23}, {ID : 113, name : 'Doe',age : 21}, {ID : 114, name : 'Stan',age : 24}, ], 2222 : [ {ID : 222, name : 'Sara',age : 15}, {ID : 223, name : 'Wiliams',age : 61}, {ID : 224, name : 'Alan',age : 45}, ], } const idsToDelete = new Set([112,223,114]); const result = Object.keys(clientData).reduce((acc, key) => { acc[key] = clientData[key].filter(({ID}) => !idsToDelete.has(ID)); return acc; }, {}); console.log('Result:', result)
 .as-console-wrapper { max-height: 100% !important; }

You can loop your clientData object with for....in loop.

 let clientData = { 1111 : [ {ID : 112, name : 'John',age : 23}, {ID : 113, name : 'Doe',age : 21}, {ID : 114, name : 'Stan',age : 24}, ], 2222 : [ {ID : 222, name : 'Sara',age : 15}, {ID : 223, name : 'Wiliams',age : 61}, {ID : 224, name : 'Alan',age : 45}, ], }; const dataToDelete = [112, 223, 114]; for (const key in clientData) { clientData[key].forEach((data,index) =>{ if (dataToDelete.includes(data.ID)){ clientData[key].splice(index,1) } }) } console.log(clientData);

Here is a straightforward way to do it.

 const deleteIds = [112,223,114]; const clientData = { 1111: [{ ID: 112, name: 'John', age: 23 }, { ID: 113, name: 'Doe', age: 21 }, { ID: 114, name: 'Stan', age: 24 } ], 2222: [{ ID: 222, name: 'Sara', age: 15 }, { ID: 223, name: 'Wiliams', age: 61 }, { ID: 224, name: 'Alan', age: 45 } ] }; for (let key in clientData) { clientData[key] = clientData[key].filter( user => !deleteIds.includes(user.ID) ); } console.log(clientData);

The key to tackling a problem like this is breaking it down into smaller tasks. For instance, clientData[1111] is an array that you want to remove something from so you can go google "how to remove an item from an array in javascript" and learn how to do that in isolation. Then come back and see if the problem makes any more sense and decide what sub task to go learn about next.

Because you've got two arrays inside an object, it appears more complex but the only complexity that that adds is in figuring out how to loop over the values (arrays in this case) in an object which is another sub task you can learn about in isolation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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