0

I want to create an object from a nested array only 2 levels deep. The function I came up with returns

[{ key1: 'value1', key2: 'value2' }]

rather than

[{ key1: 'value1' }, {key2: 'value2' }]

I've also tried replacing the if statement with obj[elem[0]] = elem[1]; but get the same result.

How can I create separate objects for each nested array?

var array = [["key1", "value1"], ["key2", "value2"]]

function nestedArrToObj(array){

  let obj = {};

  for (let i=0; i < array.length;i++) {
    let elem = array[i];

    if (!(obj[elem[0]])) {
      obj[elem[0]] = elem[1]
    }
  }

  return [obj];
}

3 Answers 3

2

Using the function map

let array = [["key1", "value1"], ["key2", "value2"]];
let result = array.map(([key, value]) => ({[key]: value}));

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

Using the function reduce

let array = [["key1", "value1"], ["key2", "value2"]];
let result = array.reduce((a, c) => {
  let [key, value] = c;
  return [...a, {[key]: value}];
}, []);

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

Cross-browser and backend technology compatible approach:

var array = [["key1", "value1"], ["key2", "value2"]];

var result = [];
for(var a of array) {
  var value = a.pop();
  var key = a.pop();
  
  var object = {};
  object[key] = value;
  
  result.push(object);
}

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

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

5 Comments

I appreciate the response but I was trying to get each nested array as a separate object
So for the answer using reduce in this line - let [key, value] = c; - you can "unpack" the array in c as separate array objects? like I could use c[0] in place of key in your answer?
@e1v1s that's called Destructuring assignment, basically get the index 0 and 1 and assign those values to key and value - Destructuring assignment
Actually, map is more appropriate here than reduce, because it's an N->N and not N->1 transform.
@georg you're right, I updated the answer. It's really straightforward with function map.
1

Here's a version with map:

console.log(
  [["key1", "value1"], ["key2", "value2"]]
      .map(x => {let obj = {}; obj[x[0]] = x[1]; return obj;})
)

2 Comments

Yep, .map is correct here, In ES6: a.map(([k, v]) => ({[k]: v}))
@georg thanks! I was looking for how to assign the key like that, lol
0

Some of the newer features of the language make this a bit easier. This is an ES5 solution.

var array = [["key1", "value1"], ["key2", "value2"]]

function nestedArrToObj(array){
  return array.reduce(function(previous, current) {
      let obj = {};
      obj[current[0]] = current[1];
      previous.push(obj);
      return previous;
  }, []);
}

console.log(nestedArrToObj(array))

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.