过滤嵌套 Object 和 return 值以及 parent 键
Filter Nested Object and return the value along with the parent keys
我有一个带有嵌套 Objects 的数组,动态键类似于此
const arr = [
{
"x1": [
{
"x1-point1": [
{
"value": 200,
"angle": 20
},
{
"value": 23,
"angle": 90
}
]
},
{
"x1-point2": [
{
"value": 54,
"angle": 0
}
]
},
{
"x1-point3": [
{
"value": 79
}
]
}
]
},
{
"x2": [
{
"x2-point1": [
{
"value": 12
}
]
},
{
"x2-point2": [
{
"value": 24
}
]
},
{
"x2-point3": [
{
"value": 36
}
]
}
]
}
]
我正在尝试搜索值并获取 parent 键以及结果 child
我试过的是这个
val = 200
arr.filter(r => !!Object.values(r).find(t => t.value == val))
我的预期结果是
[
{
"x1": [
{
"x1-point1": [
{
"value": 200,
"angle": 20
}
]
}
]
}
]
我在这里做错了什么,我过滤了与字符串匹配的最里面的 child 并获取它的 parent 键
下面介绍的是实现所需 objective 的一种可能方法。
代码段
// find needle "n" in hayStack "hs"
const myFind = (n, hs, res=[]) => {
// search "hs" array for value "n"
const foundIt = hs.find(ob => 'value' in ob && ob.value === n);
// if found, add it to "res" array & return
if (foundIt) {
res.push(foundIt);
return res;
} else {
// not-found - so, go to inner/nested level
// hold result in "ires" - intermediate result array
let ires = [];
// for each object "ob" in "hs" array
// iterate over "ob"'s key-value pairs
// filter out pairs where "v" is not an array
hs.forEach(ob => {
Object.entries(ob)
.filter(([k, v]) => v && Array.isArray(v))
.forEach(([k, v]) => {
// make recursive call, this time "v" is the hayStack
const ores = myFind(n, v);
// if recursion result is present, accumulate it to "ires"
if (ores && ores.length > 0) {
ires.push({ [k] : ores });
};
})
});
// return if there is intermediate result
if (ires.length > 0) return ires;
};
// value "n" not found
return false;
};
const arr = [{
"x1": [{
"x1-point1": [{
"value": 200,
"angle": 20
},
{
"value": 23,
"angle": 90
}
]
},
{
"x1-point2": [{
"value": 54,
"angle": 0
}]
},
{
"x1-point3": [{
"value": 79
}]
}
]
},
{
"x2": [{
"x2-point1": [{
"value": 12
}]
},
{
"x2-point2": [{
"value": 24
}]
},
{
"x2-point3": [{
"value": 36
}]
}
]
}
];
console.log('find value: 200 result: ', myFind(200, arr));
console.log('find value: 54 result: ', myFind(54, arr));
console.log('find value: 36 result: ', myFind(36, arr));
console.log('find value: 205 result: ', myFind(205, arr));
.as-console-wrapper { max-height: 100% !important; top: 0 }
说明
在上面的代码段中添加了内联评论。
PS:如果您想为 Whosebug 社区增加价值,
请考虑阅读:What to do when my question is answered
谢谢!
我有一个带有嵌套 Objects 的数组,动态键类似于此
const arr = [
{
"x1": [
{
"x1-point1": [
{
"value": 200,
"angle": 20
},
{
"value": 23,
"angle": 90
}
]
},
{
"x1-point2": [
{
"value": 54,
"angle": 0
}
]
},
{
"x1-point3": [
{
"value": 79
}
]
}
]
},
{
"x2": [
{
"x2-point1": [
{
"value": 12
}
]
},
{
"x2-point2": [
{
"value": 24
}
]
},
{
"x2-point3": [
{
"value": 36
}
]
}
]
}
]
我正在尝试搜索值并获取 parent 键以及结果 child 我试过的是这个
val = 200
arr.filter(r => !!Object.values(r).find(t => t.value == val))
我的预期结果是
[
{
"x1": [
{
"x1-point1": [
{
"value": 200,
"angle": 20
}
]
}
]
}
]
我在这里做错了什么,我过滤了与字符串匹配的最里面的 child 并获取它的 parent 键
下面介绍的是实现所需 objective 的一种可能方法。
代码段
// find needle "n" in hayStack "hs"
const myFind = (n, hs, res=[]) => {
// search "hs" array for value "n"
const foundIt = hs.find(ob => 'value' in ob && ob.value === n);
// if found, add it to "res" array & return
if (foundIt) {
res.push(foundIt);
return res;
} else {
// not-found - so, go to inner/nested level
// hold result in "ires" - intermediate result array
let ires = [];
// for each object "ob" in "hs" array
// iterate over "ob"'s key-value pairs
// filter out pairs where "v" is not an array
hs.forEach(ob => {
Object.entries(ob)
.filter(([k, v]) => v && Array.isArray(v))
.forEach(([k, v]) => {
// make recursive call, this time "v" is the hayStack
const ores = myFind(n, v);
// if recursion result is present, accumulate it to "ires"
if (ores && ores.length > 0) {
ires.push({ [k] : ores });
};
})
});
// return if there is intermediate result
if (ires.length > 0) return ires;
};
// value "n" not found
return false;
};
const arr = [{
"x1": [{
"x1-point1": [{
"value": 200,
"angle": 20
},
{
"value": 23,
"angle": 90
}
]
},
{
"x1-point2": [{
"value": 54,
"angle": 0
}]
},
{
"x1-point3": [{
"value": 79
}]
}
]
},
{
"x2": [{
"x2-point1": [{
"value": 12
}]
},
{
"x2-point2": [{
"value": 24
}]
},
{
"x2-point3": [{
"value": 36
}]
}
]
}
];
console.log('find value: 200 result: ', myFind(200, arr));
console.log('find value: 54 result: ', myFind(54, arr));
console.log('find value: 36 result: ', myFind(36, arr));
console.log('find value: 205 result: ', myFind(205, arr));
.as-console-wrapper { max-height: 100% !important; top: 0 }
说明
在上面的代码段中添加了内联评论。
PS:如果您想为 Whosebug 社区增加价值,
请考虑阅读:What to do when my question is answered 谢谢!