Ayokoke
Ayokoke

Reputation: 31

Save arrays as value in keys and loop over them to get key

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);
    }
  }
}

Upvotes: 3

Views: 75

Answers (3)

mplungjan
mplungjan

Reputation: 178285

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))

Upvotes: 2

vitaly-t
vitaly-t

Reputation: 25890

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));

Upvotes: 0

KooiInc
KooiInc

Reputation: 122936

@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))

Upvotes: 0

Related Questions