【发布时间】:2021-11-25 10:37:16
【问题描述】:
我的 JSON 结构是这样的
var data = var data = [{"id": 451, "title": { "rendered": "title1" }, "acf": { "floor": "6", "business": [ "business" ], "status": { "value": "null", "label": "Määramata" }, "suund": "", "area": { "value": "full", "label": "Terve korrus" }, "suurus": "", "tookohad": "", }, },];
我需要将 acf 展平才能在没有 acf 嵌套的情况下得到它。
var data = [
{
"id": 451,
"title": {
"rendered": "Title 1"
},
"floor": "6",
"business": [
"business"
],
"status": {
"value": "null",
"label": "Määramata"
},
"suund": "",
"area": {
"value": "full",
"label": "Full"
},
"size": "",
},
];
我尝试使用这个功能
function flat(source, target) {
Object.keys(source).forEach(function (k) {
if (source[k]!== null && typeof source[k] === 'object') {
flat(source[k], target);
return;
}
target[k] = source[k];
});flatObject = {};flat(data, flatObject);console.log(flatObject);
但它会使一切变平。我需要从 acf 中挑选一些东西,但要保持带有 id 的元素分开。
【问题讨论】:
标签: javascript json flatten