JavaScript循环遍历对象以将数组附加到数组

时间:2022-11-29 20:51:50

I spent all night working on this, and I know the answer is close; but I still have a lot of syntax work to do. Please do not downvote this - doing so only discourages newbies like me from asking questions. I need to create a function that accepts simple objects as an argument, and returns an array of arrays. Function should work on objects with any number of columns of properties with a simple string/number value. I'll show and explain my work in the codes I'll attach below:

我整夜都在为此工作,我知道答案很接近;但是我还有很多语法工作要做。请不要低估这一点 - 这样做只会阻止像我这样的新手提问。我需要创建一个接受简单对象作为参数的函数,并返回一个数组数组。函数应该处理具有任意数量的属性列的对象,并且具有简单的字符串/数字值。我将在下面附上的代码中展示和解释我的工作:

//my first code:
var hi = {a: 1, b: 2, c: 3};
var props = new Array([],[],[]);
var lent = 0;
function newWave(hi){
  for(var key in hi){
    props[lent].push(key);
    props[lent].push(hi[key]);
    lent = lent + 1;
  }
  return props;
}
newWave(hi);

//function yields: [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
//The return output is correct, but I need a universal code; 
//looking at the number of columns in variable 'props', you can 
//tell that this function only works on objects with 3 roperties. 

//My second code:
function newWave(hi) {
  var props = [];
  var outcome = [];
  for (var i = 0; i<1; i++){
    for(var key in hi){
      props.push(key, hi[key]);
      outcome.push(props);
    }
    return outcome;
  }
}
newWave(hi);
//This returns:
//[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ],
//  [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ],
//  [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
//I feel like the answer is so close, but it's been 7 hours working on this,
//your help is greatly appreciated

3 个解决方案

#1


1  

function newWave (hi) {
    return Object.keys(hi).map(v => [v, hi[v]]);
}

So here we use Object.keys to get an array of the object properties, and then for each of those we map them to an array thay has the property as first item and its value as second

所以在这里我们使用Object.keys来获取对象属性的数组,然后对于每个我们将它们映射到数组,我们将属性作为第一项,其值作为第二项

#2


1  

You don't have to define array before, you can just create new empty array in your function, loop passed object and push [key, value]

你不必先定义数组,你可以在你的函数中创建新的空数组,循环传递对象并按[key,value]

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  var r = []
  for (var key in obj) r.push([key, obj[key]])
  return r;
}

console.log(JSON.stringify(newWave(hi)))

Also there is new js method called Object.entries() that can return this result.

还有一个名为Object.entries()的新js方法可以返回此结果。

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  return Object.entries(obj)
}

console.log(newWave(hi))

#3


0  

Use Object.keys to get all the keys present in the Object in an array. Loop over that array and push the keys and value to another array

使用Object.keys获取数组中Object中存在的所有键。遍历该数组并将键和值推送到另一个数组

var hi = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
var finalArray = []

function newWave(hi) {
  var getKeys = Object.keys(hi);
  for (var i = 0; i < getKeys.length; i++) {
    var tempArray = [];
    tempArray.push(getKeys[i].toString(), hi[getKeys[i]])
    finalArray.push(tempArray)
  }
  return finalArray;
}

console.log(newWave(hi));

#1


1  

function newWave (hi) {
    return Object.keys(hi).map(v => [v, hi[v]]);
}

So here we use Object.keys to get an array of the object properties, and then for each of those we map them to an array thay has the property as first item and its value as second

所以在这里我们使用Object.keys来获取对象属性的数组,然后对于每个我们将它们映射到数组,我们将属性作为第一项,其值作为第二项

#2


1  

You don't have to define array before, you can just create new empty array in your function, loop passed object and push [key, value]

你不必先定义数组,你可以在你的函数中创建新的空数组,循环传递对象并按[key,value]

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  var r = []
  for (var key in obj) r.push([key, obj[key]])
  return r;
}

console.log(JSON.stringify(newWave(hi)))

Also there is new js method called Object.entries() that can return this result.

还有一个名为Object.entries()的新js方法可以返回此结果。

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  return Object.entries(obj)
}

console.log(newWave(hi))

#3


0  

Use Object.keys to get all the keys present in the Object in an array. Loop over that array and push the keys and value to another array

使用Object.keys获取数组中Object中存在的所有键。遍历该数组并将键和值推送到另一个数组

var hi = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
var finalArray = []

function newWave(hi) {
  var getKeys = Object.keys(hi);
  for (var i = 0; i < getKeys.length; i++) {
    var tempArray = [];
    tempArray.push(getKeys[i].toString(), hi[getKeys[i]])
    finalArray.push(tempArray)
  }
  return finalArray;
}

console.log(newWave(hi));