Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing selected objects from array of objects

What I’m doing wrong when I try to get some objects into an array of objects based in another array of objects (that is result of selection of user):

//Original array
let obj1 = [
    {"Nome": "João", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Pedro", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Marcos", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Lucas", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Tiago", "Idade": 16, "Fone 1": "55 5555"}
];
//Array selected by the user to delete:
let obj2 = [
    {"Nome": "Marcos", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Lucas", "Idade": 16, "Fone 1": "55 5555"},
];

function deleteObj(val1, val2){
    let val = [];
    for (const i of val1) {
        for (const x of val2) {
            JSON.stringify(i) !== JSON.stringify(x) ? val.push(i) : {};
        }
    }
    return val;
}
console.log(deleteObj(obj1, obj2));

I’ll use this code to remove the values selected by the user in the JSON file, but, when I run the result is:

[
  { Nome: 'João', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'João', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Pedro', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Pedro', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Marcos', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Lucas', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Tiago', Idade: 16, 'Fone 1': '55 5555' },
  { Nome: 'Tiago', Idade: 16, 'Fone 1': '55 5555' }
]

Seems that the "non-selected" values are added too.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.filter the first array, by checking if the second array contains objects with the same Nome.

//Original array
let obj1 = [
    {"Nome": "João", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Pedro", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Marcos", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Lucas", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Tiago", "Idade": 16, "Fone 1": "55 5555"}
];
//Array selected by the user to delete:
let obj2 = [
    {"Nome": "Marcos", "Idade": 16, "Fone 1": "55 5555"},
    {"Nome": "Lucas", "Idade": 16, "Fone 1": "55 5555"},
];

function deleteObj(val1, val2){
  return val1.filter(
    row => !val2.some(toRemove => row.Nome === toRemove.Nome)
  );
}

console.log(deleteObj(obj1, obj2));

If necessary, you can add extra fields to the equality check:

return val1.filter(
  row => !val2.some(toRemove =>
    row.Nome === toRemove.Nome &&
    row.Idade === toRemove.Idade &&
    row['Fone 1'] === toRemove['Fone 1'])
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading