skip to Main Content

I want to be able to use getValue and iterate myObject keys to get the key value "f" if i input any variable.

result = "f"; 

Or

result = " g";
const myObject = {
  "f": [1, 2, 3, 4, 5],
  "g": [6, 7, 8, 9, 10]
};
let getValueOne = 1;

function getKeyByValue() {
  for (let i = 0; i < myObject[value].length; i++) {
    result = myObject.key[i];
    if (i === getValueOne) {
      console.log(result);
    }
  }
}

3

Answers


  1. You mean find the key which array contains the value?

    const getByValue = (obj,val) => Object.entries(obj)
      .filter(([key,arr]) => arr.includes(val))
      .map(([key,arr]) => key)[0] ?? "N/A";
    const myObject = {
      "f": [1, 2, 3, 4, 5],
      "g": [6, 7, 8, 9, 10]
    };
    console.log(getByValue(myObject,1))
    console.log(getByValue(myObject,99))
    console.log(getByValue(myObject,6))

    Alternative

    const getByValue = (obj,val) => Object.entries(obj)
      .reduce((acc,[key,arr]) => (arr.includes(val) && acc.push(key),acc),[])[0] ?? "N/A";
      
    const myObject = {
      "f": [1, 2, 3, 4, 5],
      "g": [6, 7, 8, 9, 10]
    };
    console.log(getByValue(myObject,1))
    console.log(getByValue(myObject,99))
    console.log(getByValue(myObject,6))

    Using a lookup table (inspired by vitaly-t’s answer)

    This is assuming unique values across all arrays

    const makeLookup = obj => Object.entries(obj).reduce((acc,[key,arr]) => (arr.forEach(val => acc[val] = key),acc),{});
    const getByValue = (tbl,val) => tbl[val] ?? "N/A";
    const myObject = {
      "f": [1, 2, 3, 4, 5],
      "g": [6, 7, 8, 9, 10]
    };
    
    const lookUp = makeLookup(myObject);
    console.log(JSON.stringify(lookUp))
    
    console.log(getByValue(lookUp,1))
    console.log(getByValue(lookUp,99))
    console.log(getByValue(lookUp,6))
    Login or Signup to reply.
  2. @mplungjans answer using Array.find

    const getByValue = (obj, val) => 
    (Object.entries(obj).find(([key, arr]) => 
       arr.find(v => val === v)) || [`N/A`]).shift();
    const myObject = {
      f: [1, 2, 3, 4, 5],
      g: [6, 7, 8, 9, 10]
    };
    console.log(getByValue(myObject,1))
    console.log(getByValue(myObject,99))
    console.log(getByValue(myObject,6))
    Login or Signup to reply.
  3. If the speed of accessing the same object is important, then it is best to create a Map from that object first, and then you can retrieve each value instantly:

    const myObject = {
        "f": [1, 2, 3, 4, 5],
        "g": [6, 7, 8, 9, 10]
    };
    
    const qm = new Map(Object.entries(myObject)
                       .flatMap(([key, val]) => val.map(a => ([a, key]))));
    
    const getByValue = (val) => qm.get(val) ?? 'N/A';
    
    console.log(getByValue(1)); //=> f
    console.log(getByValue(99)); //=> N/A
    console.log(getByValue(6)); //=> g
    

    And if some values are not unique across arrays, each later value will be overriding the former one.

    const myObject = {
    "f": [1, 2, 3, 4, 5],
    "g": [6, 7, 8, 9, 10]
    };
    
    const qm = new Map(Object.entries(myObject)
    .flatMap(([key, val]) => val.map(a => ([a, key]))));
    
    const getByValue = (val) => qm.get(val) ?? 'N/A';
    
    console.log(getByValue(1));
    console.log(getByValue(99));
    console.log(getByValue(6));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search