-1

I am very new to the javascript part I have one array of objects (this is a constant object) and another response is coming from the API which contains an array of objects,now I want to merge a constant array of objects inside the response objects

//constant array
var temp = [{
            'key1' : 'valu1',
            'key2' : 'val2'
           }];

response_from_api =  [
        {
          
            "created_date": "2022-07-28",
            "tank_type": "2"
        },
        {
            
            "created_date": "2022-07-28",
            "tank_type": "1"
        },

I want to merge temp array inside the response_from_api array of each object

AntonioSk
  • 528
  • 3
  • 20
sai sa
  • 171
  • 1
  • 5
  • can you explain what you mean by merge? Is it just adding the objects from one array to the other? – raz-ezra Jul 28 '22 at 08:53
  • I think if you just show how the expected result should be, instead of trying to explain it with your words, it will be easier for anyone to understand – Diego D Jul 28 '22 at 08:57
  • What have you tried so far? It is expected that you show some effort, Please see the [ask](https://stackoverflow.com/help/how-to-ask) help page and [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – dippas Jul 28 '22 at 08:58
  • add your expected result to code – Moein Moeinnia Jul 28 '22 at 08:59
  • Does this answer your question? [How to concatenate properties from multiple JavaScript objects](https://stackoverflow.com/questions/2454295/how-to-concatenate-properties-from-multiple-javascript-objects) – dippas Jul 28 '22 at 09:39

5 Answers5

1

You can simply use the Spread Operator.

const temp = [{
  'key1': 'valu1',
  'key2': 'val2'
}];

const response_from_api = [{

    "created_date": "2022-07-28",
    "tank_type": "2"
  },
  {

    "created_date": "2022-07-28",
    "tank_type": "1"
  },
]

const newArray = [
  ...temp,
  ...response_from_api
];

console.log(newArray);

Hope this will help you.

ertu68
  • 43
  • 4
1

If I understand the question correctly, then you want to add the temp values inside each object. In that case, you can do something like this:

var temp = {
            key1 : 'valu1',
            key2 : 'val2',
           };

const response_from_api =  [
        {
          
            created_date: "2022-07-28",
            tank_type: "2"
        },
        {
            
            created_date: "2022-07-28",
            tank_type: "1"
        },
];

const mergedArray = response_from_api.map(res => {
    return { ...res, ...temp }
})

console.log(mergedArray);

Hope it helps.

user3195845
  • 401
  • 1
  • 5
  • 14
0

you can merge both something like

var temp = [{
            'key1' : 'valu1',
            'key2' : 'val2'
           }];

response_from_api =  [
        {
          
            "created_date": "2022-07-28",
            "tank_type": "2"
        },
        {
            
            "created_date": "2022-07-28",
            "tank_type": "1"
        },
        ]
        
        console.log([...temp, ...response_from_api])
Umair Mateen
  • 171
  • 4
0

Make a new variable and mutate (copy) the previous objects and "merge" them together in a new variable newArr

const temp = { key1: "valu1", key2: "val2" };
const response = [
  {
    created_date: "2022-07-28",
    tank_type: "2"
  },
  {
    created_date: "2022-07-28",
    tank_type: "1"
  }
];

const newArr = [temp, ...response];
Frikk
  • 54
  • 4
0

Use concat.

const temp = [{
  'key1': 'valu1',
  'key2': 'val2'
}];

const response_from_api = [{

    "created_date": "2022-07-28",
    "tank_type": "2"
  },
  {

    "created_date": "2022-07-28",
    "tank_type": "1"
  },
]

const newArray = temp.concat(response_from_api);

console.log(newArray);
JMP
  • 4,417
  • 17
  • 30
  • 41