我正在努力操纵JS中的树对象,它可能很简单,但我找不到。
这是对象:
{
"100-silk": {
"url": "https://www.striiiipes.com/product-category/100-silk/"
},
"apparel": {
"url": "https://www.striiiipes.com/product-category/apparel/"
},
"ipad-accessories": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/",
"ipad-air": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-air/"
},
"ipad-mini": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-mini/"
},
"ipad-pro": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/",
"ipad-pro-12-9": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/ipad-pro-12-9/"
},
"ipad-pro-9-7": {
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/ipad-pro-9-7/"
}
}
}
我正在使用一个简单的递归函数来解决所有孩子:
function parseCategories(a) {
if (typeof a === 'object') {
$.each(a, function (i, v) {
//if we only have a "url" object, then we're on last branch and can parse it
if (Object.keys(v).length === 1 && 'url' in v) {
//dosomething
} else { // then we need to go deeper in the tree
parseCategories(v);
}
})
}
}
我想使用此功能来保存面包屑并获得如下对象:
"100-silk": {
"categories" : ["100-silk"],
"url": "https://www.striiiipes.com/product-category/100-silk/"
},
"apparel": {
"categories" : ["apparel"],
"url": "https://www.striiiipes.com/product-category/apparel/"
},
"ipad-accessories": {
"categories" : ["ipad-accessories"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/",
"ipad-air": {
"categories" : ["ipad-accessories", "ipad-air"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-air/"
},
"ipad-mini": {
"categories" : ["ipad-accessories", "ipad-mini"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-mini/"
},
"ipad-pro": {
"categories" : ["ipad-accessories", "ipad-pro"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/",
"ipad-pro-12-9": {
"categories" : ["ipad-accessories", "ipad-pro", "ipad-pro-12-9"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/ipad-pro-12-9/"
},
"ipad-pro-9-7": {
"categories" : ["ipad-accessories", "ipad-pro", "ipad-pro-9-7"],
"url": "https://www.striiiipes.com/product-category/ipad-accessories/ipad-pro/ipad-pro-9-7/"
}
}
}
};
所以重点是保存每个孩子和父母的完整面包屑。我该如何更改解析函数来获取此对象?我试图在每个循环中也传递父级,但我无法完全得到我想要的东西......
我在这里创建了jsfiddle。
非常感谢你的帮助!
答案 0 :(得分:1)
您可以检查嵌套对象的属性并指定类别值。
如果
,它可以使用支票object[k]
是一个真正的价值,typeof object[k] === 'object'
,如果类型是对象,则!Array.isArray(object[k])
,如果实际属性不是数组,例如新添加的category
,然后它使用之前级别的类别并为其添加实际键来分配新属性category
。
function updateCategory(object) {
Object.keys(object).forEach(function (k) {
if (object[k] && typeof object[k] === 'object' && !Array.isArray(object[k])) {
object[k].category = (object.category || []).concat(k);
updateCategory(object[k]);
}
});
}
var data = { "100-silk": { url: "URL/100-silk/" }, apparel: { url: "URL/apparel/" }, "ipad-accessories": { url: "URL/ipad-accessories/", "ipad-air": { url: "URL/ipad-accessories/ipad-air/" }, "ipad-mini": { url: "URL/ipad-accessories/ipad-mini/" }, "ipad-pro": { url: "URL/ipad-accessories/ipad-pro/", "ipad-pro-12-9": { url: "URL/ipad-accessories/ipad-pro/ipad-pro-12-9/" }, "ipad-pro-9-7": { url: "URL/ipad-accessories/ipad-pro/ipad-pro-9-7/" } } } };
updateCategory(data);
console.log(data);

.as-console-wrapper { max-height: 100% !important; top: 0; }