Javascript 查找对象中与给定字符串匹配的所有键的路径
我有一个对象,在不同的深度有重复的键名。我想编写一个函数(纯JS),它返回一个路径数组,指向与给定名称匹配的所有键 例如:Javascript 查找对象中与给定字符串匹配的所有键的路径,javascript,ecmascript-6,Javascript,Ecmascript 6,我有一个对象,在不同的深度有重复的键名。我想编写一个函数(纯JS),它返回一个路径数组,指向与给定名称匹配的所有键 例如: const obj = { stuff: { A: 'text' }, deeperStuff: { nested: { A: ['data'] } } }; const func = (object, key) => { ... }; console.
const obj = {
stuff: {
A: 'text'
},
deeperStuff: {
nested: {
A: ['data']
}
}
};
const func = (object, key) => {
...
};
console.log(func(obj, 'A'));
// [['stuff', 'A'], ['deeperStuff', 'nested', 'A']]
抱歉,如果这是一个重复的问题,但我找不到适合我需要的答案
提前感谢,
像这样的东西怎么样。。。获取对象并通过其关键帧循环以查找匹配项。如果我们找到一个,则添加到哈希(我更改了输出类型)。如果该值是另一个对象,则使用该嵌套对象递归
const myObj={
材料:{
A:‘文本’
},
深水凝灰岩:{
嵌套的:{
答:[“数据”]
}
}
};
函数findKeys(obj,desiredKey,path=[],results={}){
常量键=对象键(obj)
键。forEach(功能(键){
const currentPath=path.concat([key])
如果(键===desiredKey){
结果[currentPath.join('/')]=obj[key]
}
if(对象类型[键]=“对象”){
findKeys(obj[key],desiredKey,currentPath,results)
}
})
返回结果
}
//与所需的返回类型具有相同的功能
函数findKeyArray(obj,desiredKey,路径=[],结果=[])){
常量键=对象键(obj)
键。forEach(功能(键){
const currentPath=path.concat([key])
如果(键===desiredKey){
results.push(currentPath);//更改了此行
}
if(对象类型[键]=“对象”){
findKeyArray(obj[key],desiredKey,currentPath,results);//这一行;)
}
})
返回结果
}
console.log(findKeys(myObj,“A”))
log(findKeyArray(myObj,“A”))
像这样的东西怎么样。。。获取对象并通过其关键帧循环以查找匹配项。如果我们找到一个,则添加到哈希(我更改了输出类型)。如果该值是另一个对象,则使用该嵌套对象递归
const myObj={
材料:{
A:‘文本’
},
深水凝灰岩:{
嵌套的:{
答:[“数据”]
}
}
};
函数findKeys(obj,desiredKey,path=[],results={}){
常量键=对象键(obj)
键。forEach(功能(键){
const currentPath=path.concat([key])
如果(键===desiredKey){
结果[currentPath.join('/')]=obj[key]
}
if(对象类型[键]=“对象”){
findKeys(obj[key],desiredKey,currentPath,results)
}
})
返回结果
}
//与所需的返回类型具有相同的功能
函数findKeyArray(obj,desiredKey,路径=[],结果=[])){
常量键=对象键(obj)
键。forEach(功能(键){
const currentPath=path.concat([key])
如果(键===desiredKey){
results.push(currentPath);//更改了此行
}
if(对象类型[键]=“对象”){
findKeyArray(obj[key],desiredKey,currentPath,results);//这一行;)
}
})
返回结果
}
console.log(findKeys(myObj,“A”))
log(findKeyArray(myObj,“A”))
您可以使用迭代和递归方法,而无需存储结果的实际访问路径
函数getDeeperPath(对象,键){
返回对象
.条目(对象)
.减少((r[k,v])=>{
如果(k==键){
返回r.concat(k);
}
if(v&&typeof v=='object'){
getDeeperPath(v,key).forEach(t=>r.push([k,…t]);
}
返回r;
}, []);
}
const object={stuff:{A:'text'},deeperStuff:{nested:{A:['data']}};
log(getDeeperPath(对象“A”)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以使用迭代和递归方法,而无需存储结果的实际访问路径
函数getDeeperPath(对象,键){
返回对象
.条目(对象)
.减少((r[k,v])=>{
如果(k==键){
返回r.concat(k);
}
if(v&&typeof v=='object'){
getDeeperPath(v,key).forEach(t=>r.push([k,…t]);
}
返回r;
}, []);
}
const object={stuff:{A:'text'},deeperStuff:{nested:{A:['data']}};
log(getDeeperPath(对象“A”)代码>
.as控制台包装{max height:100%!important;top:0;}
这里是一个使用的解决方案。简洁,允许在搜索路径中使用复杂的模式,但是您添加了一个依赖项-因此这里有一个折衷
//const objectScan=require('object-scan');
constmyobj={stuff:{A:'text'},deeperStuff:{nested:{A:['data']}};
log(objectScan(['**.A'])(myObj));
//=>[['deeperStuff','nested','A'],['stuff','A']]
log(objectScan(['**.stuff.A'])(myObj));
//=>
。作为控制台包装{最大高度:100%!重要;顶部:0}
下面是一个使用的解决方案。简洁,允许在搜索路径中使用复杂的模式,但是您添加了一个依赖项-因此这里有一个折衷
//const objectScan=require('object-scan');
constmyobj={stuff:{A:'text'},deeperStuff:{nested:{A:['data']}};
log(objectScan(['**.A'])(myObj));
//=>[['deeperStuff','nested','A'],['stuff','A']]
log(objectScan(['**.stuff.A'])(myObj));
//=>
。作为控制台包装{最大高度:100%!重要;顶部:0}
这些解决方案的可能副本看起来会起作用,但我更喜欢简单的JS解决方案仔细阅读该问题下的注释。我仍然看不到简单的JS解决方案。费里斯的回答击中了这个问题的要害,所以我接受了这是一个线索,而不是解决办法。解决方案发布在答案中,而不是评论中。这些解决方案的可能副本看起来会起作用,但我更喜欢简单的JS solutionRead评论