1

Using Array of array to object - javascript and Convert an array to an array of objects as a guide I tried to write a function that results in taking this array of array list:

var list = [['name', 'Sparky'], ['breed', 'lab'], ['age', 4]];

and converting it into this:

{
  name : 'Sparky'
  breed : 'lab',
  age : 4
}

However, when I run the code below:

var list = [
  ['name', 'Sparky'],
  ['breed', 'lab'],
  ['age', 4]
];

function toObjects(data) {
  var keys = data.shift(),
    i = 0,
    k = 0,
    obj = null,
    output = [];

  for (i = 0; i < data.length; i++) {
    obj = {};

    for (k = 0; k < keys.length; k++) {
      obj[keys[k]] = data[i][k];
    }

    output.push(obj);
  }

  return output;
}
var data = [
  ['name', 'Sparky'],
  ['breed', 'lab'],
  ['age', 4]
];

console.log(toObjects(data))

I get this:

console.log(toObjects(data)); //=> 

[ { name: 'breed', Sparky: 'lab' },{ name: 'age', Sparky: 4 } ]

but I need the output to not have an extra array [ ] and to also be listed single file like this:

       {
          name : 'Sparky'
          breed : 'lab',
          age : 4
        }

Any advice? Please and thank you!

2
  • Then define output as {} and instead of obj[..][..] do output[..][..] Commented Feb 20, 2017 at 6:14
  • Can't you do list.reduce((accu, curVal) => { accu[curVal[0]] = curVal[1]; return accu; }, {})? Commented Feb 20, 2017 at 6:18

2 Answers 2

2

I would use Array.prototype.reduce to perform your transformation as well as argument destructing. Take a look, the code is pretty simple :)

let list = [
  ['name', 'Sparky'],
  ['breed', 'lab'],
  ['age', 4]
];

function toObjects(data) {
  // reduce iterates through every element in the `data` array
  // and accumulates it into the object

  let initialValue = {};
  return data.reduce((obj, [key, value]) => {
    obj[key] = value;
    return obj;
  }, initialValue); // this could be in-lined with `{}`
}

var data = [
  ['name', 'Sparky'],
  ['breed', 'lab'],
  ['age', 4]
];

console.log(toObjects(list))

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

3 Comments

@Rajesh I just wanted to name it something to show what's going on. I'll rename it again
Thanks @Rico, this is what I was looking for!!
Thanks everyone for your help!! Appreciate it!
0

You can use map function to achieve this

var list = [['name', 'Sparky'], ['breed', 'lab'], ['age', 4]];
var newObj = new Object();
list.forEach(function (arr) {
   newObj[arr[0]] = arr[1];
});

console.log(newObj)

4 Comments

I don't think OP is looking for alternatives. Object of this question is to get output as object and not array.
Downvote because you are using Array.map without returning anything and assigning. This is just wasting Array.map. If you just want to process array, use Array.forEach
newObj[arr[0]] = arr[1]; is used within map.
Array.map is used to map values based on current values. You are not returning anything inside .map so it will return undefined. You are not saving output anywhere as well. This is classic case for .forEach. A user with your rep should know this

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.