0

I am trying to convert this multidimensional array to object, yet when I run it I get the second part of the array saved into the object and the first part is missing.

How do I get the whole array saved in the object?

var array = [
  [
    ['name', 'John Smith'],
    ['age', 34],
    ['occupation', 'nurse']
  ],
  [
    ['name', 'Nico Klein'],
    ['age', 24],
    ['occupation', 'engineer']
  ]
];

function toObject(arr) {
  var obj = {};
  for (var j = 0; j < arr.length; j++) {
    for (var i = 0; i < arr[j].length; i++) {
      obj[arr[j][i][0]] = arr[j][i][1];
    }
  }
  return obj;
}

var result = toObject(array);

console.log(result);

And is there a better method of writing this?

3
  • 3
    It doesn't seem reasonable to create a single object from that data. An array of objects would make more sense. Commented Jan 5, 2017 at 4:13
  • 2
    well ... each 3rd level pair has name, age, occupation ... so of course you'll only get the last one ... you need an array of objects as your final output, not an object ... or use some other "key" for each "person", then within that "key" have an object Commented Jan 5, 2017 at 4:13
  • 1
    That's because arr[j][i][0] is the same value each iteration, so it just overwrites the existing value. For example for name it's doing: obj.name = 'John Smith' then obj.name = 'Nico Klein'. Commented Jan 5, 2017 at 4:18

2 Answers 2

2

You have done almost well, but the parent should be array of objects, which makes sense.

var array = [
  [
    ['name', 'John Smith'],
    ['age', 34],
    ['occupation', 'nurse']
  ],
  [
    ['name', 'Nico Klein'],
    ['age', 24],
    ['occupation', 'engineer']
  ]
];

function toObject(arr) {
  var obj = [];
  for (var j = 0; j < arr.length; j++) {
    var cur = {};
    for (var i = 0; i < arr[j].length; i++) {
      cur[arr[j][i][0]] = arr[j][i][1];
    }
    obj.push(cur);
  }
  return obj;
}

var result = toObject(array);
console.log(result);

The output will be in the order of:

[
  {
    "name": "John Smith",
    "age": 34,
    "occupation": "nurse"
  },
  {
    "name": "Nico Klein",
    "age": 24,
    "occupation": "engineer"
  }
]

And it has all the records from the original array. If you still want to convert the resultant array into an object, look at Convert Array to Object.

var array = [
  [
    ['name', 'John Smith'],
    ['age', 34],
    ['occupation', 'nurse']
  ],
  [
    ['name', 'Nico Klein'],
    ['age', 24],
    ['occupation', 'engineer']
  ]
];

function toObject(arr) {
  var obj = [];
  for (var j = 0; j < arr.length; j++) {
    var cur = {};
    for (var i = 0; i < arr[j].length; i++) {
      cur[arr[j][i][0]] = arr[j][i][1];
    }
    obj.push(cur);
  }
  return obj;
}

var result = toObject(array);
var resObj = result.reduce(function(acc, cur, i) {
  acc[i] = cur;
  return acc;
}, {});
console.log(resObj);

The final Object output will give you:

{
  "0": {
    "name": "John Smith",
    "age": 34,
    "occupation": "nurse"
  },
  "1": {
    "name": "Nico Klein",
    "age": 24,
    "occupation": "engineer"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

First of all you should try to convert that array to "array of objects", this makes sense because you have two elements in the array. So you should try to get array of objects.

Secondly this program is correct but there is a mistake. Look at your code you have created a var obj = {}; which is an object and you are assigning the array values it should be array of object so change the function as follows

function toObject(arr) {
  var arrOfObj = [];
  for (var j = 0; j < arr.length; j++) {
    var curObj = {};
    for (var i = 0; i < arr[j].length; i++) {
      curObj[arr[j][i][0]] = arr[j][i][1];
    }
    arrOfObj.push(cur);
  }
  return arrOfObj;
}

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.