更新的问题
这是jQuery中数组(数据集)的控制台输出:
[
{
"Alias_0":"Toto",
"Address_1":"Here",
"Location_0":"Bombay"
},
{
"Alias_0":"Tata",
"Address_1":"There",
"Location_0":"Berlin"
},
{
"Alias_0":"Tutu",
"Address_1":"Somewhere",
"Location_0":"Brussels"
}
]
这是我想对其进行转换的方式:
[
{
"Alias":"Toto",
"Address":"Here",
"Location":"Bombay"
},
{
"Alias":"Tata",
"Address":"There",
"Location":"Berlin"
},
{
"Alias":"Tutu",
"Address":"Somewhere",
"Location":"Brussels"
}
]
这是我的尝试:
var dataSet = [
{"Alias_0": "Toto", "Address_1": "Here", "Location_0": "Bombay"},
{"Alias_0": "Tata", "Address_1": "There", "Location_0": "Berlin"},
{"Alias_0": "Tutu", "Address_1": "Somewhere", "Location_0": "Brussels"}
];
var result=[];
$.each(dataSet, function(key1,value1){
result[key1] = new Array();
$.each(dataSet[key1], function(key2,value2){
key2=key2.slice(0,-2);
result[key1][key2] = new Array();
result[key1][key2] = value2;
})
})
console.log(result)
这就是我得到的:
[
[],
[],
[]
]
这是什么问题? 在控制台中输出时,键和值将显示预期的数据。但是“结果”数组不包括它们。 而且,新的数组格式不好(用[]代替{})
仅供参考:我更希望只从原始数组中删除键的最后一个字符,但是由于无法执行此操作,因此我试图从原始数组。但是,如果可以直接转换原始数组而不创建第二个数组,那将是理想的选择。
总而言之,仅从数组的每个键中删除两个字符似乎太复杂了。必须有一个更简单的方法(直接函数?)
谢谢。
答案 0 :(得分:1)
您可以使用.reduce():
for (int i = 0; i < num_objects; i++) {
glDrawArrays(GL_TRIANGLES,
object[n]->first_vertex,
object[n]->vertex_count);
}
答案 1 :(得分:0)
最后明白了。
var dataSet = [
{"Alias_0": "Toto", "Address_1": "Here", "Location_0": "Bombay"},
{"Alias_0": "Tata", "Address_1": "There", "Location_0": "Berlin"},
{"Alias_0": "Tutu", "Address_8": "Somewhere", "Location_0": "Brussels"}
];
var sliced_dataSet=[];
for(var i=0;i<dataSet.length;i++){
var obj={};
Object.keys(dataSet[i]).forEach(function(key){
var k = key.substring(0,key.length-2);
obj[k] = dataSet[i][key];
});
sliced_dataSet.push(obj);
}
console.log(arr)