使用array.push(obj)错误

时间:2018-02-22 17:59:21

标签: javascript arrays javascript-objects

尝试从2个不同长度的数组创建对象数组。一个数组具有key值,另一个数组为每个键存储values的数组数组:

var keyArray = ["key1","key2","key3","key4"];
var valuesArray = [{"value1","value2","value3","value4"},{"value5","value6","value7","value8"},{"value9","value10","value11","value12"}];

我正在尝试使用每个results对象创建一个新的foo数组:

var currentKey; // temp variable
var currentValue; // temp variable
var foo = {}; 
var results = [];

for(var i = 0; i < valuesArray.length; i++) {
  for(var j = 0; j < keyArray.length; j++) {
    currentKey = keyArray[j];
    currentValue = valuesArray[i][j];
    foo[currentKey] = currentValue;
  }
results.push(foo);
}
console.log(results); // results = [{key1: "value9", key2: "value10", key3: "value11", key4: "value12"},{key1: "value9", key2: "value10", key3: "value11", key4: "value12"},{key1: "value9", key2: "value10", key3: "value11", key4: "value12"}]

预期结果:

console.log(results) // results = [{key1: "value1", key2: "value2", key3: "value3", key4: "value4"},{key1: "value5", key2: "value6", key3: "value7", key4: "value8"},{key1: "value9", key2: "value10", key3: "value11", key4: "value12"}]

出于某种原因,只有valuesArray{value9,value10,value11,value12})中的最后一项被推入results数组

3 个答案:

答案 0 :(得分:1)

您一直在更新相同的foo对象。您应该在外循环的每次迭代中将新对象重新分配给foo

var keyArray = ["key1","key2","key3","key4"];
var valuesArray = [["value1","value2","value3","value4"
],["value5","value6","value7","value8"],["value9","value10","value11","value12"]];

var currentKey; // temp variable
var currentValue; // temp variable
var foo; 
var results = [];

for(var i = 0; i < valuesArray.length; i++) {
  foo = {};
  for(var j = 0; j < keyArray.length; j++) {
    currentKey = keyArray[j];
    currentValue = valuesArray[i][j];
    foo[currentKey] = currentValue;
  }
  results.push(foo);
}
console.log(results);

您还可以使用Array.map()迭代values数组,并使用Array.reduce()为每个子数组生成一个对象:

var keyArray = ["key1","key2","key3","key4"];
var valuesArray = [["value1","value2","value3","value4"
],["value5","value6","value7","value8"],["value9","value10","value11","value12"]];

var results = valuesArray.map(function(s) {
  return keyArray.reduce(function(o, k, i) {
    o[k] = s[i];
  
    return o;
  }, {});
});

console.log(results);

答案 1 :(得分:0)

您可以使用更简洁的方法,并使用Object.assign将对象映射到单个对象并返回一个对象数组。

&#13;
&#13;
var keys = ["key1", "key2", "key3", "key4"],
    values = [["value1", "value2", "value3", "value4"], ["value5", "value6", "value7", "value8"], ["value9", "value10", "value11", "value12"]],
    result = values.map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] }))));

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用函数spark.sql("insert overwrite table test select column1 from source_table") reduce

map
var keyArray = ["key1","key2","key3","key4"];
var valuesArray = [["value1","value2","value3","value4"],["value5","value6","value7","value8"],["value9","value10","value11","value12"]];

var result = valuesArray.map(a => (a.reduce((a,c,i) => ({...a, ...{ [keyArray[i]] : c}}), {})));

console.log(result);