0

I have multiple arrays, the first one contains the keys, and the others contain the values.

Array[
  "name",
  "surname",
  "city",
  "age"
],
Array[
  "John",
  "Doe",
  "San Francisco",
  "27"
],
Array[
  "Nancy",
  "Doe",
  "New York",
  "15"
],
Array[
  "Maria",
  "Doe",
  "Texas",
  "30"
],

And I want to merge the arrays into an object like this:

{
  "name": "John"
  "surname": "Doe"
  "city": "San Francisco"
  "age": "27"
},
{
  "name": "Nancy"
  "surname": "Doe"
  "city": "New York"
  "age": "15"
},
{
  "name": "Maria"
  "surname": "Doe"
  "city": "Texas"
  "age": "30"
}

How can I do it using js as the length, keys and values of the arrays can change.

7
  • 1
    What have you tried so far? Commented Mar 11, 2020 at 21:52
  • Hint: you may want to loop over Object.keys(...) of the first array Commented Mar 11, 2020 at 21:53
  • Do you have the arrays with the values in another array that you can loop over? Commented Mar 11, 2020 at 21:55
  • Your desired result is not an object, it's 3 different objects. Did you mean an array of objects? Commented Mar 11, 2020 at 21:56
  • @curiousdev Object.keys() is for objects, why would you use that on an array? Commented Mar 11, 2020 at 21:56

2 Answers 2

3

You could map over the values part and create new objects by iterating the first array as keys and take the actual value from the values part.

var data = [["name", "surname", "city", "age"], ["John", "Doe", "San Francisco", "27"], ["Nancy", "Doe", "New York", "15"], [ "Maria", "Doe", "Texas", "30"]],
    result = data
        .slice(1)
        .map(a => Object.fromEntries(data[0].map((k, i) => [k, a[i]])));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

I just have added Object.fromEntries = data => Object.assign({}, ...Array.from(data, ([k, v]) => ({[k]: v}) )); as in nodejs Object.fromEntries is not supported.
0

Your Array has semantic mistakes.

//This is your Array in JS:

let array = [
  ["name", "surname", "city", "age"],
  ["John", "Doe", "San Francisco", "27"],
  ["Nancy", "Doe", "New York", "15"],
  ["Maria", "Doe", "Texas", "30"]
];

//we need to sepparete keys and values
const keys = array.shift(); // ["name", "surname", "city", "age"]

//now array has just values and keys has the first item of an array

let arrOfObjects = array.map(item => {
  let obj = {};
  for (i = 0; i < item.length; i++) {
    obj[keys[i]] = item[i];
  }
  return obj;
});
console.log(arrOfObjects);

console:

[ { "name": "John", "surname": "Doe", "city": "San Francisco", "age": "27" }, { "name": "Nancy", "surname": "Doe", "city": "New York", "age": "15" }, { "name": "Maria", "surname": "Doe", "city": "Texas", "age": "30" } ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.