简体   繁体   English

array.push(object)未在javascript循环的末尾推送

[英]array.push(object) not pushed at the end of the loop in javascript

var temparray1 = [[1,3,4],[5,6,7],[8,9,10]];
var final = [];
var obj = {};
for(var temp in temparray1){
    for(var test in temparray1[temp]){
        obj.b = temparray1[temp][0];
        obj.c = temparray1[temp][1];
        obj.d = temparray1[temp][2];
    }
    console.log(obj);
    final.push(obj);
}

current output 电流输出

[{ b: 8, c: 9, d: 10 }
{ b: 8, c: 9, d: 10 }
{ b: 8, c: 9, d: 10 }]

expected Out put : 预期输出

[{ b: 1, c: 3, d: 4 }
{ b: 5, c: 6, d: 7 }
{ b: 8, c: 9, d: 10 }]

i am running my javascript in node.js -v 8.1.x server 我在node.js -v 8.1.x服务器中运行我的JavaScript

in the console at the end of the for loop it prints the required out put but not in the array push 在控制台中for循环的末尾,它将打印所需的输出,但不打印在数组推中

Probably, you set obj outside the for loop, therefore the props are overwritten and you push the same object multiple times into the array. 可能是将obj设置在for循环之外,因此道具被覆盖,并且将同一对象多次推入数组。 Simply move the obj declaration into the loop. 只需将obj声明移入循环即可。 And you probably just need the outer loop. 您可能只需要外部循环。

Btw much shorter: 顺便说一句:

let final = temparray1.map(
 ([b,c,d]) => ({b,c,d})
);

Here is what you wanted 这就是你想要的

var temparray1 = [[1,3,4],[5,6,7],[8,9,10]];
var final = [];
for(var temp in temparray1){
   var obj = {};
   obj['b'] = temparray1[temp][0];
   obj['c'] = temparray1[temp][1];
   obj['d'] = temparray1[temp][2];
   final.push(obj);
}
console.log(final);

Hope this helps! 希望这可以帮助!

 var temparray = [[1, 3, 4], [5, 6, 7], [8, 9, 10]]; const final = temparray.map(a => ({ b: a[0], c: a[1], d: a[2] })); console.log(final); 

You could use Array#map and return an object with the wanted properties. 您可以使用Array#map并返回具有所需属性的对象。

 var array = [[1, 3, 4], [5, 6, 7], [8, 9, 10]], result = array.map(function (a) { return { b: a[0], c: a[1], d: a[2] }; }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

With ES6, you could map the inner arrays as well with an array for the keys with Object.assign and spread syntax ... . 使用ES6,您可以使用Object.assignspread语法...映射内部数组和键数组。

 var array = [[1, 3, 4], [5, 6, 7], [8, 9, 10]], keys = ['b', 'c', 'd'], result = array.map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] })))); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暂无
暂无

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

相关问题 使用调用Angular / JavaScript * array.push(function(parameter))*中的函数的array.push时,修改数组中推送的对象 - Modify the object that is pushed in an array when using an array.push that calls a function in Angular/JavaScript *array.push(function(parameter))* Array.push 新对象,最后一个对象被推送 - Array.push new objects, last object pushed 如何在JavaScript中将对象添加到数组的末尾。 array.push接缝不适用于此处的对象 - How do i add an Object to end of an array in javascript. array.push seams not working for object here JavaScript Array.push() - Javascript Array.push() Array.push 返回推送值? - Array.push return pushed value? 如何扩展javascript中的array.push(),以便在调用push方法之后,元素不应被推入数组中? - how to extend array.push() in javascript such that after calling push method, element should not get pushed into the array? 嵌套 for 循环中的 Array.push() 导致推送父循环的最后一个值 - Array.push() in a nested for loop causing last value of parent loop to be pushed javascript array.push内部循环-结果不是数组? - javascript array.push inside for loop--result not array? js/数组| Array.push 一个带过滤器的对象进入 forEach 循环 - js/array | Array.push an object with filter into a forEach loop array.push 仅在 for 循环 javascript 中的最后一个变量 - array.push only the last variable in a for loop javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM