InsideDarkWeb.com

merge Map and Array of objects by key

I Have next map:

 const filter = new Map();

  filter.set('a1', {
    Day: 55,
    Type: 1,
  });

  filter.set('a2', {
    Day: 2,
    Type: 3,
  });

And next Array:

Data = [
    {
      points: 1,
      event: 'a1',
    },
    {
      points: 2,
      event: 'a2',
    },
  ]

I am new in JS, so it is not clear for me, how I can merge these by event parameter?
Expected output should be Map:

   result = ['a1',
    {
      points: 1,
      Day: 55,
      Type: 1,
    }],
    ['a2',
    {
      points: 2,
      Day: 2,
      Type: 3,
    }],

Stack Overflow Asked on November 22, 2021

2 Answers

2 Answers

you don't need reduce here. better use map with es6 spread ... operator for merging,

const filter = new Map();

filter.set('a1', {
  Day: 55,
  Type: 1,
});

filter.set('a2', {
  Day: 2,
  Type: 3,
});

const Data = [{
    points: 1,
    event: 'a1',
  },
  {
    points: 2,
    event: 'a2',
  },
]

const result = Data.map(o => [o.event, {
  points: o.points,
  ...filter.get(o.event)
}]);

console.log(result);

Answered by Thomas on November 22, 2021

You could do something like the following:

const filter = new Map();

filter.set("a1", {
  Day: 55,
  Type: 1
});
filter.set("a2", {
  Day: 2,
  Type: 3
});

const data = [
  { points: 1, event: "a1" },
  { points: 2, event: "a2" }
];

const final = data.reduce((accumulator, item) => {
  // use destructing assignment to unpack values from the object
  const { event, points } = item;
  
  // get the appropriate event from the `filter` map by its event id
  const details = filter.get(event);

  // return a new array with the existing elements in `accumulator`
  // by using the `spread syntax` and append a new item/array
  // that has the `event` id in the first place, and an object
  // with the rest of the details in second place
  return [
    ...accumulator,
    [
      event,
      {
        points,
        ...details
      }
    ]
  ];
  // start with an empty array
}, []);

console.log(final);

References:

Answered by goto1 on November 22, 2021

Add your own answers!

Related Questions

How to render an element within a function in React?

3  Asked on January 28, 2021 by peppa-box

   

3 errors on function from vue js

1  Asked on January 28, 2021 by nicolas-vanhuynegem

       

variadic template, Range, C++

1  Asked on January 28, 2021 by hrant-nurijanyan

         

AWS Lambda import module error in python

21  Asked on January 28, 2021 by nithin-k-anil

     

React native maps rerender on marker press

1  Asked on January 28, 2021 by enguerranws

       

Python: overwrite text file – string placeholder

1  Asked on January 28, 2021 by thommy-bee

       

Wrangling dataframes with Dplyr

1  Asked on January 28, 2021

     

How to Deserialize a list of objects from json in flutter

8  Asked on January 28, 2021 by gainz

     

How to pass input variable in structure directive?

1  Asked on January 27, 2021 by alice-messis

 

How to iterate over a list of floats in python

4  Asked on January 27, 2021 by joeyhoward988

 

Can I run docker system prune -a without downtime

1  Asked on January 27, 2021 by aswin-george

 

Calculating total Price of groceries with execptions in Python

4  Asked on January 27, 2021 by shemtheultimate

 

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.