简体   繁体   English

JavaScript遍历对象以将数组追加到数组

[英]JavaScript loop through object to append arrays to arrays

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 

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来获取对象属性的数组,然后将它们映射到数组,将属性作为第一项,并将其值作为第二项

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)) 

Use Object.keys to get all the keys present in the Object in an array. 使用Object.keys获取数组中Object中存在的所有键。 Loop over that array and push the keys and value to another array 遍历该数组并将键和值推到另一个数组

 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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM