JavaScript从数组数组创建RETURN对象的函数

时间:2021-03-20 12:53:03

This is for an assignment, I've worked on this for hours to no avail. Please DO NOT vote down - I was told that even the experts ask questions time to time:

这是一项任务,我已经工作了几个小时无济于事。请不要投票 - 有人告诉我,即使是专家也会不时地提出问题:

I need to create a function to take in an array of arrays as n argument, and 'return' an object as shown at the end of my code below. (*'s filters make it awkward for me to ask a fluent question)

我需要创建一个函数来接受一个数组数组作为n参数,并'return'一个对象,如下面代码末尾所示。 (*的过滤器让我很难问一个流畅的问题)

Function must use 'return' and NOT 'console.log'. Here's my code:

函数必须使用'return'而不​​是'console.log'。这是我的代码:

var orders = [
    [
        ['first', 1], ['second', 2], ['third', 3], ['fourth', 4]
    ],
    [
        ['why', 'yes'], ['this', 'no'], ['if', 'so'], ['tit', 'tat']
    ]
];

function arrayToObject(array) {
  obj = {};
  for (var i= 0; i< array.length; i++){
    for (var j= 0; j< array[i].length; j++){
      obj[array[i][j][0]] = array[i][j][1];
    }
    return obj;
  } 
}
arrayToObject(orders);
//This function returns:
//{first: 1, second: 2, third: 3, fourth: 4}

//However I need it to return this array:
//[{first: 1, second: 2, third: 3, fourth: 4}
//{why: 'yes', this: 'no', if: 'so, tit: 'tat'}]

Your help would be greatly appreciated. Thanks again.

非常感谢您的帮助。再次感谢。

3 个解决方案

#1


0  

Not sure if this is the output that you need. This converts each array element to an object, then returns an array of the objects created.

不确定这是否是您需要的输出。这会将每个数组元素转换为一个对象,然后返回所创建对象的数组。

var orders = [
    [
        ['first', 1], ['second', 2], ['third', 3], ['fourth', 4]
    ],
    [
        ['why', 'yes'], ['this', 'no'], ['if', 'so'], ['tit', 'tat']
    ]
];

function arrayToObject(array) {
  var result = [];
  for (var i= 0; i< array.length; i++){
    var obj = {};//obj holds the object that the next element in the array is converted to.
    for (var j= 0; j< array[i].length; j++){
      obj[array[i][j][0]] = array[i][j][1];
    }
    result.push(obj); //add obj to a result once we converted all elements of array[i]
  } 
  return result; //what is returned needs to be out of the for loop
}
arrayToObject(orders);

//output:  
//[ { first: 1, second: 2, third: 3, fourth: 4 },
  { why: 'yes', this: 'no', if: 'so', tit: 'tat' } ]

#2


0  

How about something like that... I've converted your code to EcmaScript6 and also moved return statement after the first loop. To return everything in once, I converted obj to an array and push each object in it separately.

这样的事情......我已经将你的代码转换为EcmaScript6,并在第一个循环之后移动了return语句。为了一次性返回所有内容,我将obj转换为数组并分别推送其中的每个对象。

var orders = [
  [
    ['first', 1], ['second', 2], ['third', 3], ['fourth', 4]
  ],
  [
    ['why', 'yes'], ['this', 'no'], ['if', 'so'], ['tit', 'tat']
  ]
];

function arrayToObject(array) {
  let obj = [];
  for (let current_list of array){
    let _tmpObj = {};
    for (let values of current_list){
      _tmpObj[values[0]] = values[1];
    }
    obj.push(_tmpObj);
  }
  return obj;
}

console.log(arrayToObject(orders));

It's output:

[
    { first: 1, second: 2, third: 3, fourth: 4 },
    { why: 'yes', this: 'no', if: 'so', tit: 'tat' }
]

#3


0  

The return statement should be below the for statements, not inside. Your code is exiting the function before it gets the the second array.

return语句应该在for语句之下,而不是在inside语句之下。您的代码在获取第二个数组之前退出该函数。

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

#1


0  

Not sure if this is the output that you need. This converts each array element to an object, then returns an array of the objects created.

不确定这是否是您需要的输出。这会将每个数组元素转换为一个对象,然后返回所创建对象的数组。

var orders = [
    [
        ['first', 1], ['second', 2], ['third', 3], ['fourth', 4]
    ],
    [
        ['why', 'yes'], ['this', 'no'], ['if', 'so'], ['tit', 'tat']
    ]
];

function arrayToObject(array) {
  var result = [];
  for (var i= 0; i< array.length; i++){
    var obj = {};//obj holds the object that the next element in the array is converted to.
    for (var j= 0; j< array[i].length; j++){
      obj[array[i][j][0]] = array[i][j][1];
    }
    result.push(obj); //add obj to a result once we converted all elements of array[i]
  } 
  return result; //what is returned needs to be out of the for loop
}
arrayToObject(orders);

//output:  
//[ { first: 1, second: 2, third: 3, fourth: 4 },
  { why: 'yes', this: 'no', if: 'so', tit: 'tat' } ]

#2


0  

How about something like that... I've converted your code to EcmaScript6 and also moved return statement after the first loop. To return everything in once, I converted obj to an array and push each object in it separately.

这样的事情......我已经将你的代码转换为EcmaScript6,并在第一个循环之后移动了return语句。为了一次性返回所有内容,我将obj转换为数组并分别推送其中的每个对象。

var orders = [
  [
    ['first', 1], ['second', 2], ['third', 3], ['fourth', 4]
  ],
  [
    ['why', 'yes'], ['this', 'no'], ['if', 'so'], ['tit', 'tat']
  ]
];

function arrayToObject(array) {
  let obj = [];
  for (let current_list of array){
    let _tmpObj = {};
    for (let values of current_list){
      _tmpObj[values[0]] = values[1];
    }
    obj.push(_tmpObj);
  }
  return obj;
}

console.log(arrayToObject(orders));

It's output:

[
    { first: 1, second: 2, third: 3, fourth: 4 },
    { why: 'yes', this: 'no', if: 'so', tit: 'tat' }
]

#3


0  

The return statement should be below the for statements, not inside. Your code is exiting the function before it gets the the second array.

return语句应该在for语句之下,而不是在inside语句之下。您的代码在获取第二个数组之前退出该函数。

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