在值中搜索 [英] Searching in values
本文介绍了在值中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个包含对象的对象中进行搜索
I want to search in an object that has objects inside it
我的对象看起来像这样:
My object looks like this:
let foo = {
65:{
foo: 'bar',
bar: 'amet',
dolor: 'foo'
},
66:{
foo: 'foo',
bar: 'sit',
dolor: 'amet'
},
70:{
foo: 'amet',
lorem: 'amet',
bar: 'sit'
}
}
我想要一个搜索函数来搜索 foo 中所有对象的所有值然后返回结果数量列表
I want a search function that searches through all values of all objects inside foo And then return a list of numbers of results
例如如果我搜索o",我希望返回:
e.g. if I search for 'o' I want this to be returned:
[65,66]
因为 65 &66 在它们的值中有 'o'
Because the 65 & 66 has 'o' inside their values
推荐答案
您可以从对象中获取键并通过给定的字符串过滤嵌套值.
You could get the keys from the object and filter the nested values by the given string.
const
getKeys = (object, string) => Object
.keys(object)
.filter(k => Object.values(object[k]).some(v => v.includes(string))),
foo = { 65: { foo: 'bar', bar: 'amet', dolor: 'foo' }, 66: { foo: 'foo', bar: 'sit', dolor: 'amet' }, 70: { foo: 'amet', lorem: 'amet', bar: 'sit' } };
console.log(getKeys(foo, 'o'));
这篇关于在值中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文