0

I have this array's and I want to replace the data form the array1 with the data of array2 if there is some coincident, for example that replace from the array1 {"Dates":"05/25/2021","valor":"0"} the data of array2 {Dates: "05/25/2021", count: "20"} that match

array1 = [{Dates:"05/10/2021",count:"0"},{Dates:"05/11/2021",count:"0"},{Dates:"05/12/2021",count:"0"},{Dates:"05/13/2021",count:"0"},{Dates:"05/14/2021",count:"0"},{Dates:"05/15/2021",count:"0"},{Dates:"05/16/2021",count:"0"},{Dates:"05/17/2021",count:"0"},{Dates:"05/18/2021",count:"0"},{Dates:"05/19/2021",count:"0"},{Dates:"05/20/2021",count:"0"},{Dates:"05/21/2021",count:"0"},{Dates:"05/22/2021",count:"0"},{Dates:"05/23/2021",count:"0"},{Dates:"05/24/2021",count:"0"},{Dates:"05/25/2021",count:"0"},{Dates:"05/26/2021",count:"0"},{Dates:"05/27/2021",count:"0"},{Dates:"05/28/2021",count:"0"},{Dates:"05/29/2021",count:"0"},{Dates:"05/30/2021",count:"0"},{Dates:"05/31/2021",count:"0"},{Dates:"06/01/2021",count:"0"},{Dates:"06/02/2021",count:"0"},{Dates:"06/03/2021",count:"0"},{Dates:"06/04/2021",count:"0"},{Dates:"06/05/2021",count:"0"},{Dates:"06/06/2021",count:"0"},{Dates:"06/07/2021",count:"0"},{Dates:"06/08/2021",count:"0"}];

array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];

if someone can help me I will appreciate, thanks

3 Answers3

0

As a naive approach I would do it like this, is this what you are looking for ?

for(let i = 0; i < array1.length; i++) {
   for(let j = 0; j < array2.length; j++) {
      if(array1[i].Dates == array2[j].Dates)
         array1[i] = array2[j];
   }
}
Clément Cartier
  • 174
  • 2
  • 11
0

Using a combination of Array.find and Array.forEach, you can iterate through array2 and update the matching object in array1.

array2.forEach(elm => {
  let match = array1.find(x => {
    return x.Dates == elm.Dates
  });
  if(typeof(match) != "undefined")
    match.count = elm.count;
});

Full snippet:

array1 = [{"Dates":"05/10/2021",count:"0"},{"Dates":"05/11/2021",count:"0"},{"Dates":"05/12/2021",count:"0"},{"Dates":"05/13/2021",count:"0"},{"Dates":"05/14/2021",count:"0"},{"Dates":"05/15/2021",count:"0"},{"Dates":"05/16/2021",count:"0"},{"Dates":"05/17/2021",count:"0"},{"Dates":"05/18/2021",count:"0"},{"Dates":"05/19/2021",count:"0"},{"Dates":"05/20/2021",count:"0"},{"Dates":"05/21/2021",count:"0"},{"Dates":"05/22/2021",count:"0"},{"Dates":"05/23/2021",count:"0"},{"Dates":"05/24/2021",count:"0"},{"Dates":"05/25/2021",count:"0"},{"Dates":"05/26/2021",count:"0"},{"Dates":"05/27/2021",count:"0"},{"Dates":"05/28/2021",count:"0"},{"Dates":"05/29/2021",count:"0"},{"Dates":"05/30/2021",count:"0"},{"Dates":"05/31/2021",count:"0"},{"Dates":"06/01/2021",count:"0"},{"Dates":"06/02/2021",count:"0"},{"Dates":"06/03/2021",count:"0"},{"Dates":"06/04/2021",count:"0"},{"Dates":"06/05/2021",count:"0"},{"Dates":"06/06/2021",count:"0"},{"Dates":"06/07/2021",count:"0"},{"Dates":"06/08/2021",count:"0"}];

array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];

array2.forEach(elm => {
  let match = array1.find(x => {
    return x.Dates == elm.Dates
  });
  if(typeof(match) != "undefined")
    match.count = elm.count;
});

console.log(array1);
Liftoff
  • 24,717
  • 13
  • 66
  • 119
0

A way to only iterate each array once is to create a Map from array2 using Dates as keys , then map the first array using Object.assign().

If the get() in the assign method is undefined (no such date stored) it will just ignore the undefined and return the original values of array1 object.

This avoids all the additional array looping using methods like find() or nested for loops

const a2Map = new Map(array2.map(e => [e.Dates, e]))

const res = array1.map(e => Object.assign({}, e, a2Map.get(e.Dates)));

console.log(res);
<script>
array1 = [{"Dates":"05/10/2021",count:"0"},{"Dates":"05/11/2021",count:"0"},{"Dates":"05/12/2021",count:"0"},{"Dates":"05/13/2021",count:"0"},{"Dates":"05/14/2021",count:"0"},{"Dates":"05/15/2021",count:"0"},{"Dates":"05/16/2021",count:"0"},{"Dates":"05/17/2021",count:"0"},{"Dates":"05/18/2021",count:"0"},{"Dates":"05/19/2021",count:"0"},{"Dates":"05/20/2021",count:"0"},{"Dates":"05/21/2021",count:"0"},{"Dates":"05/22/2021",count:"0"},{"Dates":"05/23/2021",count:"0"},{"Dates":"05/24/2021",count:"0"},{"Dates":"05/25/2021",count:"0"},{"Dates":"05/26/2021",count:"0"},{"Dates":"05/27/2021",count:"0"},{"Dates":"05/28/2021",count:"0"},{"Dates":"05/29/2021",count:"0"},{"Dates":"05/30/2021",count:"0"},{"Dates":"05/31/2021",count:"0"},{"Dates":"06/01/2021",count:"0"},{"Dates":"06/02/2021",count:"0"},{"Dates":"06/03/2021",count:"0"},{"Dates":"06/04/2021",count:"0"},{"Dates":"06/05/2021",count:"0"},{"Dates":"06/06/2021",count:"0"},{"Dates":"06/07/2021",count:"0"},{"Dates":"06/08/2021",count:"0"}];

array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];

</script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150