简体   繁体   中英

get key of element in array which is inside object

I have array which has key and inside an 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.

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

 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:

 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

 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. This function matches the first time the code is found.

 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 . You can iterate over an object with for..of or for..in . I used for..in , because that one extracts the property name by default.

Read more about for..in on MDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM