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.

10
  • 1
    have the objects (with same values) the same object reference? Commented Jul 30, 2019 at 18:08
  • Why don't you just assign it to whatever you want and add it after? Commented Jul 30, 2019 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? Commented Jul 30, 2019 at 18:08
  • @Shidersz the id's are identical yes. For example the id of { id: "a" } in both arrays is exactly the same. Commented Jul 30, 2019 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 Commented Jul 30, 2019 at 18:11

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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]?
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;.
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);

1 Comment

@TylerRoper, please see edit.
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.