1

I have two arrays, "calls" and "Othercalls" with some contact details. Now I wanted to compare these two arrays and update the "status" property to false in "calls" array. The status value should only change in contact those are in "Othercalls" array.

What I tried here is,

These are my two arrays.

this.state = {
      calls: [
        {
          fullname: 'Mark Doe',
          phoneNumber: '0112234567',
          status: true
        },
        {
          fullname: 'Clark Man',
          phoneNumber: '0723434567',
          status: true
        },
        {
          fullname: 'Jaden Boor',
          phoneNumber: '0778902356',
          status: true
        },
        {
          fullname: 'Srick Tree',
          phoneNumber: '0980234589',
          status: true
        },
        {
          fullname: 'John Doe',
          phoneNumber: '0112255644',
          status: true
        },
        {
          fullname: 'Mark Doe',
          phoneNumber: '0723567890',
          status: true
        },
        {
          fullname: 'John Doe',
          phoneNumber: '0778904321',
          status: true
        },
        {
          fullname: 'Mark Doe',
          phoneNumber: '0785674334',
          status: true
        },
        {
          fullname: 'Jaden Boor',
          phoneNumber: '0713456980',
          status: true
        },
        {
          fullname: 'Mark Doe',
          phoneNumber: '0112357654',
          status: true
        },
      ],
      Othercalls: [
        {fullname: 'Mark Doe', phoneNumber: '0112234567'},
        {fullname: 'Clark Man', phoneNumber: '0723434567'},
        {fullname: 'Jaden Boor', phoneNumber: '0778902356'},
      ]
    };

And here I got the details of "othercalls" array and change the status into false. But this is not what I want. I need to compare these two arrays and update the status value to false only in contacts that are in "othercalls" array. And also I need the full data of "calls" array.

let tempList = this.state.Othercalls.map(item => item.phoneNumber);
    let result = this.state.calls.filter(item => (tempList.includes(item.phoneNumber)))
    result.map((listItem, index) => {
      listItem.status = false
    })

I want this solution to solve my problem here: (How to compare two array in react native?)

Sidath
  • 379
  • 1
  • 9
  • 25

1 Answers1

2

You should do something like below to update the calls array

 const data = this.state.calls.map(item => {
    if (this.state.Othercalls.find(contact => contact.phoneNumber === item.phoneNumber)) {
      item.status = false;
    }
    return item;
  });

The data will have the updated array, you can set it to the state if necessary.

Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50