两个数组到对象数组的对象

时间:2017-01-06 07:11:05

标签: javascript

我有这样的数据:

=ADDRESS(ROW()-1, COLUMN()-1)

我想将其转换为:

var data = {
  value1: ["a", "b", "c"],
  value2: [1, 2, 3]
}

如何使用javascript实现此目的?

5 个答案:

答案 0 :(得分:9)

使用Array#map方法生成数组。

var data = {
  value1: ["a", "b", "c"],
  value2: [1, 2, 3]
};

// iterate over the first property
var res = data.value1.map(function(v, i) {
  // generate the array object element
  return {
    value1: v,
    value2: data.value2[i]
  }
});

console.log(res);

更新1:如果属性名称未知,那么您可以执行以下操作。

var data = {
  value1: ["a", "b", "c"],
  value2: [1, 2, 3]
};

// get all property names
var keys = Object.keys(data);

// iterate over the first property name array
var res = data[keys[0]].map(function(v, i) {
  // generate the object by iterating over the keys array
  return keys.reduce(function(obj, k) {
    // define the object property
    obj[k] = data[k][i];
    // return the objet reference
    return obj;
    // set initial value as empty object
  }, {});
});

console.log(res);

更新2:如果数组长度不同,则需要从集合中获取更大的数组。

var data = {
  value1: ["a", "b", "c", "d"],
  value2: [1, 2, 3],
  value3: [1, 2, 3, 5, 6, 7, 8]
};

// get all property names
var keys = Object.keys(data);

// iterate over the largest array
var res = data[
  // get the largest array holding key from the keys array  
  keys.reduce(function(k, k1) {
    return data[k1].length > data[k].length ? k1 : k;
  })
].map(function(v, i) {
  // generate the object by iterating over the keys array
  return keys.reduce(function(obj, k) {
    // check element exist in the array by comparing
    // the length and index
    if (data[k].length > i)
    // define the property if value exist in index
      obj[k] = data[k][i];
    // return the objet reference
    return obj;
    // set initial value as empty object
  }, {});
});

console.log(res);

答案 1 :(得分:4)

您可以使用完整的动态版本,而无需提供密钥。

它适用于长度不等的数组。

function pivot(object) {
    return Object.keys(object).reduce(function (r, k) {
        object[k].forEach(function (a, i)  {
            r[i] = r[i] || {};
            r[i][k] = a;
        });
        return r;
    }, []);
}

console.log(pivot({ value1: ["a", "b", "c"], value2: [1, 2, 3] }));
console.log(pivot({ value1: ["a", "b", "c", "d"], value2: [1, 2, 3], value3: [1, 2, 3, 5, 6, 7, 8] }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:3)

var data = {
  value1: ["a", "b", "c"],
  value2: [1, 2, 3]
}

var newData  = [];


for (var i=0; i<data.value1.length; i++) {
      var obc = {}
          obc['value1'] = data.value1[i];
          obc['value2'] = data.value2[i];
      newData.push(obc)
}

console.log(newData)

答案 3 :(得分:0)

试试这个:

  var data = {
        value1: ["a", "b", "c"],
        value2: [1, 2, 3]
    };
    
    var arr = [];
    
    data[Object.keys(data)[0]].forEach(function (value, index) {
        arr[index] = {};
        Object.keys(data).forEach(function (key) {
            arr[index][key]=data[key][index];
        });
    });
    
    console.log(arr)

使用此解决方案,您可以修改value1value2道具,也可以根据需要添加更多道具。

答案 4 :(得分:0)

var data = {
  value1: ["a", "b", "c"],
  value2: [1, 2, 3]
}

var newData = [];

for(var i=0;i<data.value1.length;i++){
   var obj = {
       value1: data.value1[i],
       value2: data.value2[i]
   }
   newData.push(obj);
}