0

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.

1
  • 224 name should be 224, name. Commented Jun 21, 2022 at 9:12

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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);

2 Comments

It works too, thank you! What about the other solution provided by @Terry Lennox? what is the best approach to achive this uquery ? :) thank you
@Duchere forEach() is faster as per the performance point of view. Here is the test suite I created jsbench.me/ksl4nyxe4g/1
1

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; }

2 Comments

It works thank you! Have you any tutorial/article about reduce function please ?
Yes indeed, the MDN docs have some good examples: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. And this one is good too: 2ality.com/2022/05/processing-arrays-non-destructively.html. Reduce can be very powerful and useful when the output array will have a different number of elements to the input, or we wish to get a sum of array element values for example.
0

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.