我的JavaScript代码中有一个像这样的JSON结构
function arraysEqual(arr1, arr2)
{
if(arr1.length !== arr2.length)
{
return false;
}
for(var i = arr1.length; i--;)
{
if(arr1[i] !== arr2[i])
{
if ( !( Array.isArray( arr1[i] ) && Array.isArray( arr2[i] ) && arraysEqual(arr1[i], arr2[i] ) ) )
{
return false;
}
}
}
return true;
}
function unite() {
var args = [].slice.apply(arguments);
return args.reduce(function(result, current){
//console.log(result, current)
current.forEach( function(item){
if ( Array.isArray ( item ) )
{
result.filter( function( res ){
return Array.isArray ( res ) && arraysEqual( item, res );
}).length == 0 && result.push( item );
}
else
{
result.indexOf( item ) == -1 && result.push( item );
}
}) ;
return result;
}, []);
}
console.log( unite([1, 3, 2], [5, [7], 4, 4], [2, 1, [7]]) );
这是从[{"id":10,"latitude":"49","longitude":"2","altitude":"construction","name":"sjiajnjdksaa","description":"jfis","urlidle":"asd","urlselected":"asd"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"}]
方法获得的,如下所示。
$.getJSON(serverUrl, function(data)
但是,我需要迭代它们中的每一个并将每一行放入一个对象,如下所示
var serverUrl = https://demo.blah.fgh/return.php?lat=20;
var jqxhr = $.getJSON(serverUrl, function(data) {
console.log(jqxhr.responseText);
答案 0 :(得分:0)
使用jQuery,
$.each(pioData, function(i, v) {
v.latitude = parseFloat(v.latitude);
...
...
...
});
因为从本质上讲,您对对象数组所做的是改变每个属性的值类型。完成迭代后,您可以再次使用pioData
,根据需要进行修改。
答案 1 :(得分:0)
你可以尝试这个经典的javascript forEach
让你的Json结构
var data=[{"id":10,"latitude":"49","longitude":"2","altitude":"construction","name":"sjiajnjdksaa","description":"jfis","urlidle":"asd","urlselected":"asd"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"},
{"id":10,"latitude":"22","longitude":"10","altitude":"asd","name":"zxcvbnmmmkk","description":"kk","urlidle":"qwerty","urlselected":"qwerty"}];
//Loop Starts
data.forEach(function(poiData){
var singlePoi = {
"id": poiData.id,
"latitude": parseFloat(poiData.latitude),
"longitude": parseFloat(poiData.longitude),
"altitude": parseFloat(poiData.altitude),
"title": poiData.name,
"description": poiData.description,
"urlidle": poiData.urlidle,
"urlselected": poiData.urlselected
};
// Now do operations with singlePoi
});
//Loop ends
答案 2 :(得分:0)
如果您希望迭代返回并格式化对象,可以执行以下操作:
function formatReturn(data) {
var formattedData = [];
data.forEach( function(poiData) {
formattedData.push(
{
"id": poiData.id,
"latitude": parseFloat(poiData.latitude),
"longitude": parseFloat(poiData.longitude),
"altitude": parseFloat(poiData.altitude),
"title": poiData.name,
"description": poiData.description,
"urlidle": poiData.urlidle,
"urlselected": poiData.urlselected
}
);
});
console.log(formattedData);
}
请注意,在这种情况下,您仍然会有一个对象数组,但纬度,经度和海拔高度属性将应用parseFloat。
如果您希望将所有行存储为对象中的属性,则可以执行以下操作:
function formatReturn(data) {
var formattedData = {};
data.forEach( function(poiData,index) {
formattedData["singlePoi_"+index] =
{
"id": poiData.id,
"latitude": parseFloat(poiData.latitude),
"longitude": parseFloat(poiData.longitude),
"altitude": parseFloat(poiData.altitude),
"title": poiData.name,
"description": poiData.description,
"urlidle": poiData.urlidle,
"urlselected": poiData.urlselected
};
}
console.log(formattedData);
}
上面将把JSON返回中的每个项目存储为formattedData对象的新属性。