Indexing nested arrays in JavaScript - Stack Overflow

I'm attempting to turn the array below into properties for an object, the problem I'm having

I'm attempting to turn the array below into properties for an object, the problem I'm having is properly using nested array notation: array[x][y]

var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];

function fromListToObject(array) {
  var result = {};
  for(var i = 0;i < array.length;i++){
    console.log(array[i]); // only here to view output
    for(var j = 0;j < array[i].length;j++){
      console.log(array[i][j]); // only here to view output
      console.log(array[i][j+1]); // only here to view output
      result[array[i][j]] = array[i][j+1];
    }
  }
  return result;
}
fromListToObject(array);

The output so far:

[ 'make', 'Ford' ] //inner array
make               //should be the key
Ford               //should be the value
Ford               //where did this e from?
undefined
[ 'model', 'Mustang' ]
model
Mustang
Mustang
undefined
[ 'year', 1964 ]
year
1964
1964
undefined
=> { '1964': undefined, //How in the heck?
  make: 'Ford',         //<--- actually what I wanted
  Ford: undefined,
  model: 'Mustang',
  Mustang: undefined,
  year: 1964 }

I did solve the assignment with forEach:

function fromListToObject(array) {
  var result = {};
  array.forEach(function(element){
    result[element[0]] = element[1];
  });
  return result;
}

and I can just capture the inner array into a temporary variable as well, so this is for my edification. Any help/hints would be greatly appreciated. Thanks.

EDIT

I just wanted to say thanks again for the answers, us rookies need guidance like yours.

I'm attempting to turn the array below into properties for an object, the problem I'm having is properly using nested array notation: array[x][y]

var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];

function fromListToObject(array) {
  var result = {};
  for(var i = 0;i < array.length;i++){
    console.log(array[i]); // only here to view output
    for(var j = 0;j < array[i].length;j++){
      console.log(array[i][j]); // only here to view output
      console.log(array[i][j+1]); // only here to view output
      result[array[i][j]] = array[i][j+1];
    }
  }
  return result;
}
fromListToObject(array);

The output so far:

[ 'make', 'Ford' ] //inner array
make               //should be the key
Ford               //should be the value
Ford               //where did this e from?
undefined
[ 'model', 'Mustang' ]
model
Mustang
Mustang
undefined
[ 'year', 1964 ]
year
1964
1964
undefined
=> { '1964': undefined, //How in the heck?
  make: 'Ford',         //<--- actually what I wanted
  Ford: undefined,
  model: 'Mustang',
  Mustang: undefined,
  year: 1964 }

I did solve the assignment with forEach:

function fromListToObject(array) {
  var result = {};
  array.forEach(function(element){
    result[element[0]] = element[1];
  });
  return result;
}

and I can just capture the inner array into a temporary variable as well, so this is for my edification. Any help/hints would be greatly appreciated. Thanks.

EDIT

I just wanted to say thanks again for the answers, us rookies need guidance like yours.

Share Improve this question edited Feb 21, 2017 at 18:39 macrolyte asked Feb 21, 2017 at 16:58 macrolytemacrolyte 3523 silver badges12 bronze badges 1
  • What's your question? You're going through each j, so you'll get what used to be j + 1 in your next iteration... – Heretic Monkey Commented Feb 21, 2017 at 17:01
Add a ment  | 

3 Answers 3

Reset to default 3

The undefined is ing from accessing an index that doesn't exists

try this

var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];

function fromListToObject(array) {
    var result = {};
    for (var i = 0; i < array.length; i++) { 
        for (var j = 0; j < array[i].length-1; j++) { // Change the condition here 
            result[array[i][j]] = array[i][j + 1];
        }
    }
    return result;
}
fromListToObject(array);

The .forEach() solution is certainly the best and clearer way to go. If you want to use traditional loops, you don't need a nested loop. You are just going through the main array and doing a single assignment for each pair. So:

function fromListToObject(array) {
  var result = {};
  for (var i = 0; i < array.length; i++) {
    result[array[i][0]] = array[i][1];
  }
  return result;
}

It would be best if you did the same thing as in your foreach solution.

Simple remove the for(var j = 0;j < array[i].length;j++){ loop because it makes no sense with what you are trying to achieve.

Use array[i][0] and array[i][1] like you do in the foreach

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745402737a4626175.html

相关推荐

  • Indexing nested arrays in JavaScript - Stack Overflow

    I'm attempting to turn the array below into properties for an object, the problem I'm having

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信