简体   繁体   中英

Javascript Compare Property Value of 2 Arrays of Objects

I need: for each property id in first array that equals id property in second array change property liked in first array from false to true. So very specifically, looking at second array we can conclude that in first array objects with id value 34, 31 and 35 will now have liked:true. How to accomplish such code?

First array:

Array [
  Object {
    "Aw8AUj1mPkON1Fd1s6LhkNETHfb2": "liked",
    "avatar": null,
    "hugCount": 2,
    "id": 35,
    "liked": false,
    "name": "fhfdhdhf",
    "text": "Yoho",
    "timestamp": 1610471860157,
    "uid": "FOgepuJqxXfkHxI8OAV2KMWodXo1",
  },
  Object {
    "avatar": null,
    "hugCount": 1,
    "id": 34,
    "liked": false,
    "mood": 2,
    "name": "fhfdhdhf",
    "text": "I'm fine today.",
    "timestamp": 1607943705709,
    "uid": "FOgepuJqxXfkHxI8OAV2KMWodXo1",
  },
  Object {
    "avatar": "https://firebasestorage.googleapis.com/v0/b/eleph-6fee9.appspot.com/o/avatars%2Fm2OnHQiDuVM3Bp40Sc2ikqqmiQz2?alt=media&token=a2d66c27-ec63-422d-a3e4-76fcf7a12134",
    "hugCount": 3,
    "id": 33,
    "liked": false,
    "mood": 3,
    "name": "Matko",
    "text": "evotv",
    "timestamp": 1606350804169,
    "uid": "m2OnHQiDuVM3Bp40Sc2ikqqmiQz2",
  }] 

and this first array goes on for long but I closed it here cause its enough to show it

Second Array:

Array [
  Object {
    "id": 34,
    "userHandle": "Aw8AUj1mPkON1Fd1s6LhkNETHfb2",
  },
  Object {
    "id": 31,
    "userHandle": "Aw8AUj1mPkON1Fd1s6LhkNETHfb2",
  },
  Object {
    "id": 35,
    "userHandle": "Aw8AUj1mPkON1Fd1s6LhkNETHfb2",
  },
]

You can use Array.prototype.some() to test if any objects in the second array have matching ids and assign the returned boolean to liked .

 const arr1 = [{ "Aw8AUj1mPkON1Fd1s6Lh.NETHfb2": "liked", "avatar": null, "hugCount": 2, "id": 35, "liked": false, "name": "fhfdhdhf", "text": "Yoho", "timestamp": 1610471860157, "uid": "FOgepuJqxXfkHxI8OAV2KMWodXo1", }, { "avatar": null, "hugCount": 1, "id": 34, "liked": false, "mood": 2, "name": "fhfdhdhf", "text": "I'm fine today.", "timestamp": 1607943705709, "uid": "FOgepuJqxXfkHxI8OAV2KMWodXo1", }, { "avatar": "https://firebasestorage.googleapis.com/v0/b/eleph-6fee9.appspot.com/o/avatars%2Fm2OnHQiDuVM3Bp40Sc2ikqqmiQz2?alt=media&token=a2d66c27-ec63-422d-a3e4-76fcf7a12134", "hugCount": 3, "id": 33, "liked": false, "mood": 3, "name": "Matko", "text": "evotv", "timestamp": 1606350804169, "uid": "m2OnHQiDuVM3Bp40Sc2ikqqmiQz2", }], arr2 = [{ "id": 34, "userHandle": "Aw8AUj1mPkON1Fd1s6Lh.NETHfb2", }, { "id": 31, "userHandle": "Aw8AUj1mPkON1Fd1s6Lh.NETHfb2", }, { "id": 35, "userHandle": "Aw8AUj1mPkON1Fd1s6Lh.NETHfb2", },] arr1.forEach(o => o.liked = arr2.some(({ id }) => o.id === id)); console.log(arr1)

This should get you the answer in O(n).

First array = va
Second array = ta

 function test(arr1, arr2){
    let em = {};
    arr2.forEach(({id}) => em[id] = 1);
    return arr1.map(el => {
        em[el.id] ? el.liked = true : null;
        return el;
    })
}

va = test(va, ta);

const arr1 = [{id: 1, liked: false}, {id: 2, liked: false}, {id: 3, liked: false}];
const arr2 = [{id: 1}, {id: 3}];

arr1.forEach(element1 => {
    element1.liked = arr2.some((element2) => {
        return element1.id === element2.id;
    });
});

Explanation:

Iterate through each array element in "arr1" (array of objects), then assign the "element1.liked" with the value returns from 'some' method. that method actually iterate through each element in "arr2" and assign it to element2 (similar to forEach) and return boolean value if the id's are equal, same boolean value that assigns to element1.liked. Hope that understandable and accurate enough.

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