如何将对象数组转换为具有不同格式的对象数组?

时间:2017-01-11 05:03:02

标签: javascript arrays

我有一个对象数组,其中我将 billkey billvalue 存储为属性。我希望将 billkey 作为密钥,将 billvalue 作为该特定密钥的值。

var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
}
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
}];

我想把它转换成这种格式:

var log=[
    {
      Name:"ABC",
      Department:"Computer"
    },
    { 
      Name:"XYZ",
      Department:"Electrical"
    }];

4 个答案:

答案 0 :(得分:0)

使用forloop

创建结果
 // store the values
  var logs=[];
  var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
},
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
},
];

循环第一个数组

for (i = 0; i < log.length; i++) {
    // create empty variable for storing the values
    var index = new Array();
    // insert the first index value to key
    index[log[i].billkey] = log[i].billvalue
    // insert the second index value to key 
    index[log[i+1].billkey] = log[i+1].billvalue
    // insert the result in to new array
    logs.push(index);
    // increment the i with 1 
    i=i+1;
}
console.log(logs);

答案 1 :(得分:0)

这个简单的解决方案怎么样?希望它有所帮助!

&#13;
&#13;
var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
},
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
}];

var arr = [];
var finalObj = [];

for(var i in log){
	var someObject = log[i];
	for(var j in someObject){
	  arr.push(someObject[j]);
	}
}

for(var k = 0; k < arr.length; k+=4){
      finalObj.push({
      Name: arr[k+1],
      Department: arr[k+3]
    });
}

console.log(finalObj);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以使用Array#reduce并使用余数运算符作为使用最后一个对象或创建一个新对象。

var log = [{ billkey: "Name", billvalue: "ABC" }, { billkey: "Department",  billvalue: "Computer" }, { billkey: "Name", billvalue: "XYZ" }, { billkey: "Department", billvalue: "Electrical" }],
    result = log.reduce(function (r, a, i) {
        var o = {};
        if (i % 2) {
            r[r.length - 1][a.billkey] = a.billvalue;
        } else {
            o[a.billkey] = a.billvalue;
            r.push(o);
        };
        return r;        
    }, []);

console.log(result);

答案 3 :(得分:-1)

为此使用简单的for循环。

&#13;
&#13;
var log = [{
  billkey: "Name",
  billvalue: "ABC"
}, {
  billkey: "Department",
  billvalue: "Computer"
}, {
  billkey: "Name",
  billvalue: "XYZ"
}, {
  billkey: "Department",
  billvalue: "Electrical"
}];

// array for storing result
var res = [];

// iterate upto the half length of the array
// since you are generating single element using 
// two adjacent elements in the array
for (var i = 0; i < log.length / 2; i++) {
  // initialize empty object
  var obj = {};
  // define first property of the object
  obj[log[2 * i].billkey] = log[2 * i].billvalue;
  // define the second property of the object
  obj[log[2 * i + 1].billkey] = log[2 * i + 1].billvalue;
  // push the object into the array
  res.push(obj);
}

console.log(res)
&#13;
&#13;
&#13;