-2

Maybe this was asked before, and I have no clue how to look for it, so I applogize in advance for my total lack of wording skills. So, here it goes. I am programming in ECMA5 (so no fancy array/object methods available). I have one array, which contains the keys, lets say:

var keys = ["name", "age", "school"];

Then, an array of arrays containing the values:

var values = [["John John", 16, "Saints Hills High School"], ["Miranda Knobs", 12, "St Mary Junior High"], ["Patricia Lee", 13, "Milwakee High School"]];

I want to create an array of objects. Each object having the keys from the first array and the values from the second array, like so:

var result = [{name: "John John", age: 16, school: "Saints Hills High School"}, {name: "Miranda Knobs", age: 12, school: "St Mary Junior High"}, {name: "Patricia Lee", age: 13, school: "Milwakee High School"}];

I saw some questions/solutions with 2 arrays, one containing the keys and one the values, but I have no idea how to repeat the first array multiple times for each object.

Iulia Mihet
  • 650
  • 1
  • 10
  • 34
  • What did you tried so far ? – Serge K. Sep 05 '17 at 09:36
  • 1/2 I do not agree with closing this Q for reasons of being a duplicate. Neither the provided link [Looping to create object Keys and Values in Javascript from 2D arrays](https://stackoverflow.com/questions/17353505/looping-to-create-object-keys-and-values-in-javascript-from-2d-arrays) nor the follow up duplicate link [Easiest way to interate over a complex JSON object via Javascript](https://stackoverflow.com/questions/17192051/easiest-way-to-interate-over-a-complex-json-object-via-javascript) provide/discuss the `Array.prototype.reduce` approach that has many advantages over classic loops. – Peter Seliger Sep 05 '17 at 10:38
  • 2/2 I really would appreciate if I could provide exactly such kind of a solution here since I made an effort answering the OP'S question and just seconds before pressing the "submit answer" button it got disabled by setting this Q to 'duplicate'. – Peter Seliger Sep 05 '17 at 10:42
  • ... never mind ... @NinaScholz meanwhile did provide two ultimate solutions to this kind of problem ... [Easiest way to interate over a complex JSON object via Javascript](https://stackoverflow.com/questions/17192051/easiest-way-to-interate-over-a-complex-json-object-via-javascript/46052774#46052774) ... now one might consider this subject as finally solved. – Peter Seliger Sep 05 '17 at 10:55
  • @PeterSeliger thank you for the links. Again, as I assumed, it was a wording issue on my side, as I didn't think about looping and 2D arrays when I searched for a solution. – Iulia Mihet Sep 05 '17 at 11:22

1 Answers1

4

You can use this code:

var keys = ["name", "age", "school"];
var values = [["John John", 16, "Saints Hills High School"], ["Miranda Knobs", 12, "St Mary Junior High"], ["Patricia Lee", 13, "Milwakee High School"]];
var resultArray = [];
for(var i=0; i<values.length; i++){
  var obj = {};
  for(var j=0; j<keys.length; j++){
     obj[keys[j]] = values[i][j];
  }
   resultArray.push(obj);
}

console.log(resultArray);

This works exactly what you want to achieve.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • This is pretty much the exact same code as in the linked duplicate. – George Sep 05 '17 at 09:45
  • @George but i don't know that as i was creating this code snippet and posted right away without having to look at the comments – Ankit Agarwal Sep 05 '17 at 09:48
  • Code-only answers aren't that helpful. You should explain why the OP has their issue (difficult when the don't provide any code) and how your code addresses it. – RobG Sep 05 '17 at 09:53
  • @AKA, thank you for the solution. I tired something very similar to your answer but did not add the notation obj[...] but something like array.push({keys[j] = values[i][j]}) and of course nothing happened. – Iulia Mihet Sep 05 '17 at 11:25
  • Glad to help you @IuliaMihet – Ankit Agarwal Sep 05 '17 at 11:28