我有一个这种格式的对象:
var request = {
"student": [
[
"name",
"age"
],
[
"Tom",
12
],
[
"Jack",
13
]
]
};
我想将其转化为:
var request = {
"student": [
{
"name": "Tom",
"age": 12
},
{
"name": "Jack",
"age": 13
}
]
}
我试过这样做:
var response = [];
var keysCount = req.result[0].length;
var responseCount = req.result.length - 1;
var i = 0,
j = 0,
key;
for (j = 0; j < responseCount; j++) {
for (i = 0; i < keysCount; i++) {
key = req.result[0][i];
response[j][key] = req.result[j + 1][i];
}
}
return response;
但是,它没有按预期工作。
答案 0 :(得分:6)
这是循环第一个数组并为所有剩余数组创建对象数组的问题,使用匹配索引处的值来创建对象的属性:
var request = {
"student": [
[
"name",
"age"
],
[
"Tom",
12
],
[
"Jack",
13
]
]
};
// Get the header array
var headers = request.student[0];
// Create the new array but mapping the other entries...
var newArray = request.student.slice(1).map(function(entry) {
// Create an object
var newEntry = {};
// Fill it in with the values at matching indexes
headers.forEach(function(name, index) {
newEntry[name] = entry[index];
});
// Return the new object
return newEntry;
});
console.log(newArray);
&#13;
答案 1 :(得分:3)
我会创建一个小函数version
,它接受一个数据数组,其中第一个元素是标题数组,其余元素是行
以下代码使用 ES6 。如果您需要ES5支持,可以使用babel等工具安全地转换此代码。
tabularize
或者您可以通过在创建对象时将表格数据直接传递到// your original data
var request = {
"student": [
[
"name",
"age"
],
[
"Tom",
12
],
[
"Jack",
13
]
]
};
// tabularize function
var tabularize = ([headers, ...rows])=>
rows.map(row=>
headers.reduce((acc,h,i)=>
Object.assign(acc, {[h]: row[i]}), {}));
// your transformed object
var request2 = {student: tabularize(request.student)};
// log the output
console.log(request2);
//=> {"student":[{"name":"Tom","age":12},{"name":"Jack","age":13}]}
函数来创建具有预期形状的request
对象
tabularize
答案 2 :(得分:2)
让我们从编写一个小函数开始,只是为了从两个数组创建一个对象,一个键和一个值:
function makeObjectFromPairs(keys, values) {
var object = {};
for (var i = 0; i < keys.length; i++) {
object[keys[i]] = values[i];
}
return object;
}
// makeObjectFromPairs(['a', 'b'], [1, 2]) === {a: 1, b: 2}
现在我们可以使用students
数组的第一个元素作为键,并将每个剩余元素用作值。
var keys = students[0];
var result = [];
for (var i = 1; i < students.length; i++) {
result.push(makeObjectFromPairs(keys, students[i]);
}
您可以使用Array#map
等作为循环的替代方法,但也许这种基本方法更易于访问。
由于您自己努力解决这个问题,让我们检查您的代码并查看您的错误。关键点在于,在开始向其添加键/值对之前,不要将输出中的每个元素初始化为空对象。
for (j = 0; j < responseCount; j++) {
// Here, you need to initialize the response element to an empty object.
response[j] = {};
答案 3 :(得分:1)
另一种解决方案:
var request = {
"student": [
[
"name",
"age"
],
[
"Tom",
12
],
[
"Jack",
13
]
]
};
var response = {};
var students = [];
var responseCount = request.student.length - 1;
var j = 0,
key;
for (j = 0; j < responseCount; j++) {
var student = {};
request.student[0].forEach(function(name, index) {
student[name] = request.student[1 + j][index];
});
students.push(student)
}
response["students"] = students;
console.log(response); // {"students":[{"name":"Tom","age":12},{"name":"Jack","age":13}]}
&#13;
答案 4 :(得分:1)
Lodash解决方案
featureArray[i] = new Point(featuresFill[2*i], featuresFill[2*i+1]);
答案 5 :(得分:-1)
简单的Javascript解决方案:
var request = {
"student": [
[
"name",
"age"
],
[
"Tom",
12
],
[
"Jack",
13
]
]
};
var students = [];
for(var x = 1; x<request.student.length;x++)
{
var temp = { 'name' : request.student[x][0],
'age' : request.student[x][1]
}
students.push(temp);
}
request = { 'students' : students}
console.log(request);