我有一个对象的javascript数组的例子,它有一个对象。所以我基本上只想把它变成一维数组。
我在SO上搜索过很多问题,但根据我的要求,我没有搜索过。
我有什么
[
{
"id": "59cf758f7bdf8d2e0c1c68c7",
"value": {
"city": "Mahbubnagar",
"state": "Andhra Pradesh",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c8",
"value": {
"city": "Udgir",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c9",
"value": {
"city": "Umarga",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68ca",
"value": {
"city": "Umarkhed",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68cb",
"value": {
"city": "Umred",
"state": "Maharashtra",
"country": "India"
}
}
]
我只是希望这个对象数组成为一个数组
[
"59cf758f7bdf8d2e0c1c68c7",
"Mahbubnagar",
"Andhra Pradesh",
"India",
],[
"59cf758f7bdf8d2e0c1c68c9",
"Umarga",
"Maharashtra",
"India",
],[
"59cf758f7bdf8d2e0c1c68ca",
"Umarkhed",
"Maharashtra",
"India",
],[
"59cf758f7bdf8d2e0c1c68c7",
"Mahbubnagar",
"Andhra Pradesh",
"India",
],
对象数组可以有更多的孩子,换句话说,你可以说它的动态内容。
到目前为止我使用的是
var object= property.value.elements;
var finalArray = object.map(function (obj) {
return obj.id;
});
console.log(finalArray);
哪个只给我ID的列表
不重复的问题请在将其标记为重复之前进行验证。
答案 0 :(得分:1)
您可以检查对象的值是否为对象,并对结果采用展平值。
function flat(object) {
return Object
.values(object)
.reduce((r, v) => r.concat(v && typeof v === 'object' ? flat(v) : v), []);
}
var array = [{ id: "59cf758f7bdf8d2e0c1c68c7", value: { city: "Mahbubnagar", state: "Andhra Pradesh", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c8", value: { city: "Udgir", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c9", value: { city: "Umarga", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68ca", value: { city: "Umarkhed", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68cb", value: { city: "Umred", state: "Maharashtra", country: "India" } }],
result = array.map(flat);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
尝试以下
var arr = [{"id":"59cf758f7bdf8d2e0c1c68c7","value":{"city":"Mahbubnagar","state":"Andhra Pradesh","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c8","value":{"city":"Udgir","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c9","value":{"city":"Umarga","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68ca","value":{"city":"Umarkhed","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68cb","value":{"city":"Umred","state":"Maharashtra","country":"India"}}];
function getVals(obj, arr) {
Object.entries(obj).forEach(([key, value]) => {
if(typeof value === "object") {
arr = [...arr, ...getVals(obj[key], arr)];
} else {
arr.push(value);
}
});
return arr;
}
var resp = arr.map((item) => {
let temp = [];
getVals(item, temp);
return temp;
});
console.log(resp);

答案 2 :(得分:0)
这不是最简单的方法。只需遍历所有对象并提取它们的值。
let x = [{"id": "59cf758f7bdf8d2e0c1c68c7", "value": {...}, ..]
let res = []
x.forEach((obj, index) => {
res.push([obj.id, obj.value.city, obj.value.state, obj.value.country])
})
console.log(res)