0

I have two identical arrays of objects.

I then add an object to one of the arrays at a random index:

Arr1 = [{ id: "a" }, { id: "b" }, { id: "c" }]

Arr2 = [{ id: "a" }, { id: "d" }, { id: "b" }, { id: "c" }]

How would I go about comparing the two arrays, extracting that new object from Arr2 and assigning that object to a constant?

This is my best effort:

const newPlan
if (state.plans.length !== data.allPlansJson.edges.length) {
    data.allPlansJson.edges.map(staticPlan => {
        state.plans.map(planInState => {
            planInState !== staticPlan && newPlan = staticPlan
        })
    })
}

Too add even more context:

I'm getting objects from the array data.allPlansJson.edges and spreading them in my a database collection. I'm then getting all of those objects and putting them back into an array in my redux state.

This function is to detect when there is a new object added to the data.allPlansJson.edges array and execute a redux action that posts this new object to my database collection.

Maximilian
  • 537
  • 7
  • 18
  • 1
    have the objects (with same values) the same object reference? – Nina Scholz Jul 30 '19 at 18:08
  • Why don't you just assign it to whatever you want and add it after? – baao Jul 30 '19 at 18:08
  • Is `id` property unique between all the objects? Or you could add an object with a duplicated `id` on the new array? – Shidersz Jul 30 '19 at 18:08
  • @Shidersz the id's are identical yes. For example the id of { id: "a" } in both arrays is exactly the same. – Maximilian Jul 30 '19 at 18:11
  • 1
    Show us what you have tried. The objective here is to help you with *your code*, not to write it all for you – charlietfl Jul 30 '19 at 18:11
  • @charlietfl I've appended my attempt to the question. I realise why this isn't working but I also can't see how to do what I want. – Maximilian Jul 30 '19 at 18:16
  • Multiple questions: **(1)** Are they references to the same object, or clones of the object? **(2)** Is "doesn't match" defined by solely the `id` being different? Or, will there be some time down the road where these objects have more properties? **(3)** Will the 'outlier' always be in `Arr2`? **(4)** If you have an array of `[{id: "A"}, {id: "B"}]`, why not just an array `let ids = ["A", "B"]`? – Tyler Roper Jul 30 '19 at 18:17
  • @TylerRoper they are clones. I'm comparing the objects solely on the id and the id will be exactly the same as the equivalent object. The objects will consist of much more than just the id key-value pair. – Maximilian Jul 30 '19 at 18:20
  • How are you cloning them? If you could share how the second array is created/populated, it would be helpful. – Tyler Roper Jul 30 '19 at 18:23
  • @TylerRoper I've added context to the question. – Maximilian Jul 30 '19 at 18:28

3 Answers3

2

For this specific scenario, in order to get just the new items you can filter out any points that are in the orignial.

const arrA = [{ id: "a" }, { id: "b" }, { id: "c" }];
const arrB = [{ id: "a" }, { id: "d" }, { id: "b" }, { id: "c" }];

const newItems = arrB.filter(b => !arrA.some(a => a.id === b.id));

arrB.filter loops over arrB calling the arrow function. To keep an item we return true in this callback. To get rid of an item we return false.

arrA.some loops over arrA calling the provided arrow function. The function will resolve true if any of the items return true. Since we are matching for items found in the array we add the ! before arrA.some in order to find items not found

You can now grab the first item from newItems by doing

const [someConst] = newItems;

Note: this is a one-way search. If there are new ones in arrA they will not be found

vivalldi
  • 511
  • 2
  • 14
  • A bit nickpicky here but is there any benefit to destructuring, i.e. (`const [someConst] = newItems;`), instead of the well-known-to-every-developer-ever `newItems[0]`? – Tyler Roper Jul 30 '19 at 18:20
  • In this scenario, no real benefit. I personally prefer it as if I'm grabbing the first item in an array, I'll probably be wanting to grab the rest to via `const [first, ...rest] = someArray;`. – vivalldi Jul 30 '19 at 18:33
1

You could find the object by checking the object with with the one at the same index.

var array1 = [{ id: "a" }, { id: "b" }, { id: "c" }],
    array2 = [{ id: "a" }, { id: "d" }, { id: "b" }, { id: "c" }],
    inserted = array2.find((o, i) => o.id !== array1[i].id);

console.log(inserted);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Arr1 = [{ id: "a" }, { id: "b" }, { id: "c" }];
Arr2 = [{ id: "a" }, { id: "d" }, { id: "b" }, { id: "c" }];

function findExtra(arr1, arr2) {
  len1 = arr1.length;
  len2 = arr2.length;
  for (i = 0; i < Math.min(len1, len2); i++) {
    if (arr1[i].id !== arr2[i].id) { // Compare elements pairwise
       return len1 > len2 ? arr1[i] : arr2[i]; // Return the newly added one
    }
  }
  // The new element is the last one
  return len1 > len2 ? arr1[len1 - 1] : arr2[len2 - 1];
}

console.log(findExtra(Arr1, Arr2));

Is this what you are looking for?

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32