-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.

5
  • What did you tried so far ? Commented Sep 5, 2017 at 9: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 nor the follow up duplicate link 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. Commented Sep 5, 2017 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'. Commented Sep 5, 2017 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 ... now one might consider this subject as finally solved. Commented Sep 5, 2017 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. Commented Sep 5, 2017 at 11:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This is pretty much the exact same code as in the linked duplicate.
@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
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.
@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.
Glad to help you @IuliaMihet

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.