如何从深层嵌套的json数组中获取值 我需要从json
下面获取所有对(nameValue和value) var json = [{
name: 'Firstgroup',
elements: [{
name: 'Field1',
elements: [{
name: 'country32',
elements: [{
nameValue: 'city1',
value: 2025
}]
}]
},
{
name: 'Field2',
elements: [{
name: 'country22',
elements: [{
nameValue: 'city2',
value: 1875
}]
},
{
name: 'country12',
elements: [{
nameValue: 'city3',
value: 1810
}]
}]
}]
},
{
name: 'Secondgroup',
elements: [{
name: 'Field1',
elements: [{
name: 'country52',
elements: [{
nameValue: 'city4',
value: 1310
},
{
nameValue: 'city5',
value: 1125
}]
}]
},
{
name: 'Field3',
elements: [{
name: 'country42',
elements: [{
nameValue: 'city6',
value: 1100
}]
}]
}]
}];
我设法通过下面这段代码获得第一对
function getDataProvider(array)
{
var dataPoint = [];
var elements = 'elements';
var name = 'nameValue';
var value = 'value';
var i, j, len;
for (j = 0; j < array.length; j++) {
i = array[j];
if (i[elements]) {
this.getDataProvider(i[elements]);
} else {
dataPoint.push({
name: i[name],
value: i[value]
});
}
}
return dataPoint;
}
我如何从上面的json中得到所有对,因为这个json是动态的,它的深度未知,但它将包含(名称值和值)对
答案 0 :(得分:2)
您可以使用迭代和递归方法在数组中获取所需的属性。
function getKeyValue(array) {
var result = [];
array.forEach(function iter(o) {
if (o.elements) {
o.elements.forEach(iter);
return;
}
result.push({ name: o.nameValue, value: o.value });
});
return result;
}
var data = [{ name: 'Firstgroup', elements: [{ name: 'Field1', elements: [{ name: 'country32', elements: [{ nameValue: 'city1', value: 2025 }] }] }, { name: 'Field2', elements: [{ name: 'country22', elements: [{ nameValue: 'city2', value: 1875 }] }, { name: 'country12', elements: [{ nameValue: 'city3', value: 1810 }] }] }] }, { name: 'Secondgroup', elements: [{ name: 'Field1', elements: [{ name: 'country52', elements: [{ nameValue: 'city4', value: 1310 }, { nameValue: 'city5', value: 1125 }] }] }, { name: 'Field3', elements: [{ name: 'country42', elements: [{ nameValue: 'city6', value: 1100 }] }] }] }],
result = getKeyValue(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
为每个递归调用返回一个数组的版本。
function getKeyValue(array) {
return array.reduce((r, o) => r.concat(
o.elements
? getKeyValue(o.elements)
: { name: o.nameValue, value: o.value }
), []);
}
var data = [{ name: 'Firstgroup', elements: [{ name: 'Field1', elements: [{ name: 'country32', elements: [{ nameValue: 'city1', value: 2025 }] }] }, { name: 'Field2', elements: [{ name: 'country22', elements: [{ nameValue: 'city2', value: 1875 }] }, { name: 'country12', elements: [{ nameValue: 'city3', value: 1810 }] }] }] }, { name: 'Secondgroup', elements: [{ name: 'Field1', elements: [{ name: 'country52', elements: [{ nameValue: 'city4', value: 1310 }, { nameValue: 'city5', value: 1125 }] }] }, { name: 'Field3', elements: [{ name: 'country42', elements: [{ nameValue: 'city6', value: 1100 }] }] }] }],
result = getKeyValue(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:2)
你可以创建一个递归函数,当它有更多元素时调用它自己,并在每次遇到结果时将值添加到它的结果中。
var getNameAndValues = function(arr) {
var nameValuePairs = [];
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
if (item.value && item.nameValue) {
nameValuePairs.push(item);
}
if (item.elements) {
nameValuePairs = nameValuePairs.concat(getNameAndValues(item.elements));
}
}
return nameValuePairs;
};
var json = [{
name: 'Firstgroup',
elements: [{
name: 'Field1',
elements: [{
name: 'country32',
elements: [{
nameValue: 'city1',
value: 2025
}]
}]
},
{
name: 'Field2',
elements: [{
name: 'country22',
elements: [{
nameValue: 'city2',
value: 1875
}]
},
{
name: 'country12',
elements: [{
nameValue: 'city3',
value: 1810
}]
}]
}]
},
{
name: 'Secondgroup',
elements: [{
name: 'Field1',
elements: [{
name: 'country52',
elements: [{
nameValue: 'city4',
value: 1310
},
{
nameValue: 'city5',
value: 1125
}]
}]
},
{
name: 'Field3',
elements: [{
name: 'country42',
elements: [{
nameValue: 'city6',
value: 1100
}]
}]
}]
}];
var result = getNameAndValues(json);
var asString = "";
for (var i = 0, len = result.length; i < len; i++) {
var item = result[i];
asString += item.nameValue + ": " + item.value + "<br/>";
}
document.body.innerHTML = asString;