0

I need to filter out contacts from data. Whatever data gets from contacts should be filtered out in remainingData

contacts = [
    {
        "contact_id": 12,
        "f_name": "RARA",
    },
    {
        "contact_id": 14,
        "f_name": "BABA",
    },
    {
        "contact_id": 15,
        "f_name": "CACA",
    },
    {
        "contact_id": 14,
        "f_name": "DADA",
    },
]


data = [
    {
        "contact_id": 12,
        "f_name": "RARA",
    },
     {
        "contact_id": 14,
        "f_name": "DADA",
    },
]


remainingData = [
     {
        "contact_id": 14,
        "f_name": "BABA",
    },
    {
        "contact_id": 15,
        "f_name": "CACA",
    },
]

CODE

const remainingData = contacts.filter(item => item.contact_id !== data.contact_id)
6
  • 1
    This is the same as your previous question. Commented Aug 1, 2021 at 11:19
  • @Ouroborus. Its different. Its array of object vs array of objects Commented Aug 1, 2021 at 11:19
  • In what way is it different? Commented Aug 1, 2021 at 11:20
  • @Ouroborus. Previous question is array of objects vs array Commented Aug 1, 2021 at 11:20
  • 1
    @Joseph see Array.prototype.filter() and Arrow function expressions docs... Commented Aug 1, 2021 at 11:26

3 Answers 3

2

Solution using array.filter

contacts = [
    { "contact_id": 12, "f_name": "RARA" },
    { "contact_id": 14, "f_name": "BABA" },
    { "contact_id": 15, "f_name": "CACA" },
    { "contact_id": 14, "f_name": "DADA" },
]


data = [ 
    { "contact_id": 12, "f_name": "RARA" },
    { "contact_id": 14, "f_name": "DADA" },
]

const remainingData = contacts.filter(item => !data.find((node) => item.contact_id === node.contact_id && item.f_name === node.f_name));

console.log(remainingData);

Alternative solution using array.reduce

contacts = [
    { "contact_id": 12, "f_name": "RARA" },
    { "contact_id": 14, "f_name": "BABA" },
    { "contact_id": 15, "f_name": "CACA" },
    { "contact_id": 14, "f_name": "DADA" },
]


data = [ 
    { "contact_id": 12, "f_name": "RARA" },
    { "contact_id": 14, "f_name": "DADA" },
]


const remainingData = contacts.reduce((acc, curr) => {
    const searchNode = data.find((node) => node.contact_id === curr.contact_id && node.f_name === curr.f_name);
    if(!searchNode) {
        acc.push(curr);
    }
    return acc;
}, []);

console.log(remainingData);

If you want the result of filtration back in the contacts variable just assign back the result of filtrarion to the same variable like

contacts = contacts.filter(item => !data.find((node) => item.contact_id === node.contact_id && item.f_name === node.f_name))
Sign up to request clarification or add additional context in comments.

1 Comment

If you are performing arrayfilter or array.reduce just re assign the contacts variable with the response of filtration like contacts = contacts.filter(item => !data.find((node) => item.contact_id === node.contact_id && item.f_name === node.f_name))
0

You can map your array of objects containing the ids to a normal array with only the ID's where you can check against

let contacts = [{
    "contact_id": 12,
    "f_name": "RARA",
  },
  {
    "contact_id": 14,
    "f_name": "BABA",
  },
  {
    "contact_id": 15,
    "f_name": "CACA",
  },
  {
    "contact_id": 14,
    "f_name": "DADA",
  },
]

let data = [{
    "contact_id": 12,
    "f_name": "RARA",
  },
  {
    "contact_id": 14,
    "f_name": "DADA",
  },
]

let filtered = contacts.filter(item => (data.map(x => (x.contact_id)).some(id => (id === item.contact_id))))

console.log(filtered)

Comments

0

To compare two objects we need to compare unique field (if exist) , so if contact_id is unique:

const remainingData = contacts.filter(item => data.find((node) => item.contact_id === node.contact_id )=='undefined');

Otherwise we should compare the two objects after converting them to json string , so :

const remainingData = contacts.filter(item => data.find((node) => JSON.stringify(item) === JSON.stringify(node))=='undefined');

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.