我希望通过“step”键的值过滤嵌套的 javascript 对象:
var data = {
"name": "Root",
"step": 1,
"id": "0.0",
"children": [
{
"name": "first level child 1",
"id": "0.1",
"step":2,
"children": [
{
"name": "second level child 1",
"id": "0.1.1",
"step": 3,
"children": [
{
"name": "third level child 1",
"id": "0.1.1.1",
"step": 4,
"children": []},
{
"name": "third level child 2",
"id": "0.1.1.2",
"step": 5,
"children": []}
]},
]}
]
};
var subdata = data.children.filter(function (d) {
return (d.step <= 2)});
这只是返回未修改的嵌套对象,即使我将过滤器的值设置为 1。.filter 是否适用于嵌套对象,或者我是否需要在此处滚动我自己的函数,建议并更正代码。cjm