Home > Enterprise >  How to find item in object by value in JS
How to find item in object by value in JS

Time:12-08

There are many questions about this, but none of them solved my problem.

I have this object:

const default_keys = {
    l: 1, 
    t: 5,
    o: 10, 
    r: 50, 
    w: 100,
    y: 500,
    m: 1000,
    f: 5000,
    a: 10000,
    z: 50000,
    d: 100000,
    q: 1000000
};

so, among all these key:value items, I want to know which key has the value = 100 (in this case it should return "w");

is it possible?

EDIT: I tried this solution, but it didn't work because it only works with fixed key names:

const object1 = {
  a: {val: "aa"},
  b: {val: "bb"},
  c: {val: "cc"}
};

let a = Object.values(object1).find((obj) => {
    return obj.val == "bb"
});

in this case the fixed name is "val", but in my case there is no fixed name..

CodePudding user response:

You could take an object with switched key/value pairs and get the key by taking the value as key.

const 
    keys = { l: 1,  t: 5, o: 10,  r: 50,  w: 100, y: 500, m: 1000, f: 5000, a: 10000, z: 50000, d: 100000, q: 1000000 },
    values = Object.fromEntries(Object.entries(keys).map(([k, v]) => [v, k]));

console.log(values[100]);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

One way would be to iterate thru Object.entries.

const default_keys = {
    l: 1, 
    t: 5,
    o: 10, 
    r: 50, 
    w: 100,
    y: 500,
    m: 1000,
    f: 5000,
    a: 10000,
    z: 50000,
    d: 100000,
    q: 1000000
};

function getKey(object, value){
  for (const [k, v] of Object.entries(object)) {
    if(v == value) return k;
  }
}

console.log(getKey(default_keys, 100));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Another way would be to use find.

const default_keys = {
  l: 1,
  t: 5,
  o: 10,
  r: 50,
  w: 100,
  y: 500,
  m: 1000,
  f: 5000,
  a: 10000,
  z: 50000,
  d: 100000,
  q: 1000000
};

function getKey(object, value) {
  let entry = Object.entries(object).find(item => item[1] == value);
  if (entry) return entry[0];
}

console.log(getKey(default_keys, 100));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here are three ways to do it:

Using for-in

const default_keys = {
  l: 1,
  t: 5,
  o: 10,
  r: 50,
  w: 100,
  y: 500,
  m: 1000,
  f: 5000,
  a: 10000,
  z: 50000,
  d: 100000,
  q: 1000000
}

// Method 1 using 'for in'
for (const key in default_keys) {
  if (default_keys[key] === 100) {
    console.log('For in: ', key)
  }
}

Using Object.keys and loops

// Method 2 using Object.keys and loops

let keys = Object.keys(default_keys)
keys.forEach(key => {
  if (default_keys[key] === 100) console.log('Using Object.keys and loops: ', key)
})

Using Object.entries

// Method 3: Functional Approach and using Object.entries
function getKey(obj) {
  for (const [key, value] of Object.entries(obj)) {
    if (value === 100) {
      return key
    }
  }
}
console.log('Functional Approach:', getKey(default_keys))

CodePudding user response:

Another easy way is to use the filter() function.

const default_keys = {
    l: 1, 
    t: 5,
    o: 10, 
    r: 50, 
    w: 100,
    y: 500,
    m: 1000,
    f: 5000,
    a: 10000,
    z: 50000,
    d: 100000,
    q: 1000000
};
console.log(Object.keys(default_keys).filter((item) => default_keys[item] == 100)[0]);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

In case you want to receive multiple results, you can remove [0] and you will get an array then.

CodePudding user response:

Using for...in:

const getKeyWithValue = (obj = {}, value) => {
  let key;
  for(const prop in default_keys) {
    if(default_keys[prop] === value) {
      key = prop;
      break;
    }
  }
  return key;
}

const default_keys = { l: 1, t: 5, o: 10,  r: 50,  w: 100, y: 500, m: 1000, f: 5000, a: 10000, z: 50000, d: 100000, q: 1000000 };
console.log( getKeyWithValue(default_keys, 100) );
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related