[英]get key of element in array which is inside object
I have array which has key and inside an object.我有一个数组,里面有一个 object。 Trying to get the key of element which is inside the array.
试图获取数组内元素的键。
when I search for 4512 I should get key as eggs_code.当我搜索 4512 时,我应该将 key 作为 egg_code。
const items = {
storeItems: {
milk_code: [1212, 9898, 1214],
vegetables_code: [2222, 75674, 8943, 3452],
eggs_code: [4543, 4512, 1754, 9090]
}
}
if (items.storeItems.includes(4512)) {
const type = Object.keys(items(4512);
console.log(type);
}
You need to iterate the key / value pairs of storeItems
, searching the values to return the key您需要迭代
storeItems
的键/值对,搜索值以返回键
const findKey = (obj, search) => { for (let [ key, val ] of Object.entries(obj)) { if (val.includes(search)) return key } return null } const items = { storeItems: { milk_code: [1212, 9898, 1214], vegetables_code: [2222, 75674, 8943, 3452], eggs_code: [4543, 4512, 1754, 9090] } } console.info(findKey(items.storeItems, 4512))
This will return the first key found (in definition order) with a matching value.这将返回找到(按定义顺序)具有匹配值的第一个键。
If the value can appear in multiple values and you want to return all matches, you can use a simple filter如果该值可以出现在多个值中并且您想返回所有匹配项,则可以使用简单的过滤器
const findKeys = (obj, search) => { return Object.keys(obj).filter(key => obj[key].includes(search)) } const items = { storeItems: { milk_code: [1212, 9898, 1214, 4512], // added 4512 here vegetables_code: [2222, 75674, 8943, 3452], eggs_code: [4543, 4512, 1754, 9090] } } console.info(findKeys(items.storeItems, 4512))
Here ya go:这里是 go:
const search_q = 4512; let k = null; const items = { storeItems: { milk_code: [1212, 9898, 1214], vegetables_code: [2222, 75674, 8943, 3452], eggs_code: [4543, 4512, 1754, 9090] } } Object.keys(items.storeItems).forEach(item => { if(items.storeItems[item].includes(search_q)) k = item; }) console.log(k)
Use Object.entries
and find
method使用
Object.entries
和find
方法
const items = { storeItems: { milk_code: [1212, 9898, 1214], vegetables_code: [2222, 75674, 8943, 3452], eggs_code: [4543, 4512, 1754, 9090], }, }; const getKey = (obj, val) => Object.entries(obj).find(([key, arr]) => arr.includes(val))?.shift(); console.log(getKey(items.storeItems, 4512));
You can write a little function to do it for you.你可以写一点 function 来为你做。 This function matches the first time the code is found.
这个 function 第一次找到代码时匹配。
const items = { store_items: { milk_code: [1212, 9898, 1214], vegetables_code: [2222, 75674, 8943, 3452], eggs_code: [4543, 4512, 1754, 9090] } } function find_code( code, object ) { for( const property in object ) if( object[property].includes(code) ) return property; return undefined; } console.log( find_code( 4512, items.store_items ) ); console.log( find_code( 1200, items.store_items ) ); console.log( find_code( 8943, items.store_items ) );
Note that you are iterating over an Object
, not an Array
.请注意,您正在迭代
Object
,而不是Array
。 You can iterate over an object with for..of
or for..in
.您可以使用
for..of
或for..in
object。 I used for..in
, because that one extracts the property name by default.我使用
for..in
,因为默认情况下会提取属性名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.