1

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 );
    console.log(nameObj);
    var tMark = Object.assign({}, mark);
    
    var finalOutput = [].concat(tName, tMark);
    
    console.log('finalOutput', finalOutput);
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
J S
  • 21
  • 6
  • I am trying to map the value of names to the value of mark. Any solution is fine. – J S Nov 04 '18 at 11:42
  • Did both arrays `name and mark` always be of equal length – front_end_dev Nov 04 '18 at 11:43
  • 1
    `name.split(',')` shouldn't be possible - you can't call `.split(...)` on an Array. – Blundering Philosopher Nov 04 '18 at 11:45
  • 1
    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? – Blundering Philosopher Nov 04 '18 at 11:48
  • Possible duplicate of [merge two arrays of keys and values to an object using underscore](https://stackoverflow.com/questions/12199051/merge-two-arrays-of-keys-and-values-to-an-object-using-underscore) – Saeed Nov 04 '18 at 11:52

2 Answers2

0

You can simply use Array.map(), 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 = names.map((name,index)=> ({[name] : mark[index]}));

console.log(result);
amrender singh
  • 7,949
  • 3
  • 22
  • 28
0

You can use Array.reduce():

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

const toObject = names.reduce((accum, item, i) => {
  accum[item] = marks[i];
  return accum;
}, {})

console.log(toObject);

Array.map() is probably more readable, but I like (and use) the reduce-object-creating pattern a lot (thanks, Wes Bos).

You might want to create function to do that, so you can reuse it:

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

const toObject = (toProp, toValue) => {
  return toProp.reduce((accum, item, i) => {
    accum[item] = toValue[i];
    return accum;
  }, {})
}

console.log(toObject(names, marks));
HynekS
  • 2,738
  • 1
  • 19
  • 34
  • Thanks it is returning into a single object [object Object] { A: "45", B: "55", C: "60", D: "65", E: "70", F: "75", G: "80", I: "85" } – J S Nov 04 '18 at 13:11