Home > other >  combining two array of objects together in reactjs
combining two array of objects together in reactjs

Time:08-07

Hello i have two arrays of objects that look like this, but it's much more bigger, the second array contains the normal history and average value per day of the same history, i want to combine the NewHistory inside the first array ( input1 ) or into combine both of them into one array of objects

const input1= const input = [{
  name: 'xy',
  lastname: 'yx',
  history: [
  { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
  { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
  { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
  { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
  { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }]
},
{
  name: 'op',
  lastname: 'po',
  history: [
  { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
  { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
  { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
  { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
  { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }]
}
]

const input2 = [{
      history: [
      { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
      { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
      { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
      { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
      { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }]
      NewHistory: [
      { "value": 0.04, "date": "2022-08-02" }, 
      { "value": 0.03, "date": "2022-08-01" }]
    },
    {
      history: [
      { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
      { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
      { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
      { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
      { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }],
      NewHistory: [
      { "value": 0.04, "date": "2022-08-02" }, 
      { "value": 0.03, "date": "2022-08-01" }]
    }
    ]

i want to combine these two arrays of objects together to get this result

const input3 = [{
      name: 'xy',
      lastname: 'yx',
      history: [
      { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
      { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
      { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
      { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
      { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }],
      NewHistory: [
      { "value": 0.04, "date": "2022-08-02" }, 
      { "value": 0.03, "date": "2022-08-01" }]
    },
    {
      name: 'op',
      lastname: 'po',
      history: [
      { "value": 0.02, "date": "2022-08-02T23:03:22.895Z" }, 
      { "value": 0.04, "date": "2022-08-02T22:03:16.603Z" },
      { "value": 0.08, "date": "2022-08-02T21:03:20.378Z" },
      { "value": 0.02, "date": "2022-08-01T23:03:32.584Z" },
      { "value": 0.04, "date": "2022-08-01T22:03:30.311Z" }]
      NewHistory: [
      { "value": 0.04, "date": "2022-08-02" }, 
      { "value": 0.03, "date": "2022-08-01" }]
    }
    ]

until now i tried to do a map through both the array but i can't seem to make it work, any input on this ?

CodePudding user response:

you can use destructuring like bellow. simple ha!

const input3 = [...input1, ...input2];

CodePudding user response:

I think the solution can simply be something like:

input1.forEach((element, i) => {element.NewHistory = input2[i].NewHistory});

  • Related