How to merge two array in one object?

2411 views javascript

I have two array name and mark

I am trying to merge two arrays as one single object so that later I can iterate over. Like there is a table of two column the first return name from API such as A, b, c, d and so and second return marks such as 40, 50 55, 60 and so on. On receiving I am trying to make it as one iterable object as

finalOutput = [
0: {
    A : 45
1: {
    B: 55
2: {
    C: 60
and so on...

I am trying to take the below approach which is not a complete solution. can you suggest me what approach I should take?

 var name = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'];
    var tName = name.split(',');
    var mark = ['45', '55', '60', '65', '70', '75', '80', '85'];
    var nameObj = Object.assign({}, tName );
    var tMark = Object.assign({}, mark);
    var finalOutput = [].concat(tName, tMark);
    console.log('finalOutput', finalOutput);

answered question

I am trying to map the value of names to the value of mark. Any solution is fine.

Did both arrays name and mark always be of equal length

name.split(',') shouldn't be possible - you can't call .split(...) on an Array.

Also your structure for finalOutput is invalid. You show an array with key-value pairs like this: [0: {A: 45} 1: {B: 55} ...], which isn't possible (and also is missing commas). You can either end with an Array like this: [{A: 45}, {B: 55}, {C: 60}, ...] or an Object like this: {A: 45, B: 55, C: 60,...} - which are you aiming for?

1 Answer


You can simply use, Assuming both your arrays have same length, and you want a array of objects as final output, try the following:

let names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'];
let mark = ['45', '55', '60', '65', '70', '75', '80', '85'];

let result =,index)=> ({[name] : mark[index]}));


posted this

Have an answer?


Please login first before posting an answer.