1

I have 2 list of maps like

payload1 = [
    {
      'series': 'abc',
      'value' : 123,
      'date'  : '2020-06-22'
    }, { 
      'series': 'efg',
      'value' : 789,
      'date'  : '2020-06-21'
    } ]

and

    payload2 = [
    {
      'series': 'xyz',
      'value' : 234,
      'date'  : '2020-06-22'
    }, { 
      'series': 'tuv',
      'value' : 23442,
      'date'  : '2020-06-21'
    } ]

How can I genrate a new list of map like

  payload = [
    {
      'series': 'abc',
      'value' : 123,
      'date'  : '2020-06-22'
    }, { 
      'series': 'efg',
      'value' : 789,
      'date'  : '2020-06-21'
    }, {
      'series': 'xyz',
      'value' : 234,
      'date'  : '2020-06-22'
    }, { 
      'series': 'tuv',
      'value' : 23442,
      'date'  : '2020-06-21'
    } ]

Basically i need to concatenate 2 list of maps to create a new one. is there any way to do this with less iteration??

1
  • 3
    const payload = [...payload1, ...payload2]? Commented Jun 23, 2020 at 15:00

2 Answers 2

7

You can use the spread operator "..." to merge the elements of two objects or arrays into one.

let payload = [...payload1, ...payload2]
Sign up to request clarification or add additional context in comments.

Comments

0

You may use the Array.concat() function.

let payload1 = [
    {
      'series': 'abc',
      'value' : 123,
      'date'  : '2020-06-22'
    }, { 
      'series': 'efg',
      'value' : 789,
      'date'  : '2020-06-21'
    } ];

    let payload2 = [
    {
      'series': 'xyz',
      'value' : 234,
      'date'  : '2020-06-22'
    }, { 
      'series': 'tuv',
      'value' : 23442,
      'date'  : '2020-06-21'
    } ];

let payload = payload1.concat(payload2);
console.log(payload);

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.