1

I would like to know how to compare object value and array value in javascript

if the array value and object value is same, then how to return the

object key value in javascript


var result = obj.values(obj).includes(arr2) ? 
'status: active' : 'status: inactive'

var obj = {
  "active": 12,
  "inactive": 14
  "neutral": 16
}

var arr1=[12]
var arr2=[12, 14]
var arr3=[12, 16]

Expected Output

//for arr1
status: active
// for arr2
status: active
status: neutral
// for arr3
status: active
status: inactive

sen
  • 91
  • 7
  • 1
    Does this answer your question? [How to iterate over a JavaScript object?](https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object) – derpirscher Jan 10 '22 at 09:36
  • 1
    Your latest edit seems to have the expected output swapped for arr2 and arr3 – Terry Jan 10 '22 at 10:07
  • @Terry, thanks for reply, for any array passed, should return the object key value if object value matches with array value – sen Jan 10 '22 at 10:09
  • Yes, then arr2 should return 'active, inactive' and arr3 should return 'active, neutral'. Your expected output is incorrectly swapped for arr2 and arr3. – Terry Jan 10 '22 at 10:10
  • What is output format should be? Is it just array`['active', 'inactive']` or array of objects`[{ status: 'active' }, { status: 'inactive' }]` ? – A1exandr Belan Jan 10 '22 at 10:13
  • @AlexandrBelan thanks for reply, array of objects – sen Jan 10 '22 at 10:19

4 Answers4

1

You can simply iterate through the array, and then attempt to fetch the [key, value] tuples returned by Object.entries(obj) whose value matches the array value. Once found, you return the key in the tuple, i.e.:

arr.map(v => Object.entries(obj).find(x => x[1] === v)[0]);

Note: If you array may contain values that are not present in the object, the code above will throw an error because .find() will return undefined. If that's the case, you need to catch cases where an invalid value is used (defensive design):

arr.map(v => {
  const foundTuple = Object.entries(obj).find(x => x[1] === v);
  return foundTuple ? foundTuple[0] : null;
});

See proof-of-concept below:

const obj = {
  "active": 12,
  "inactive": 14,
  "neutral": 16
}

const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const invalid_arr = [12, 999];

function getKeys(obj, arr) {
  return arr.map(v => {
    const foundTuple = Object.entries(obj).find(x => x[1] === v);
    return foundTuple ? foundTuple[0] : null;
  });
}

console.log(getKeys(obj, arr1));
console.log(getKeys(obj, arr2));
console.log(getKeys(obj, arr3));
console.log(getKeys(obj, invalid_arr));
Terry
  • 63,248
  • 15
  • 96
  • 118
0

You should check that the array includes the object value, not vice-versa:

function* matches(object, array) {
  for (const [key, value] of Object.entries(object)) {
    if (array.includes(value))
      yield key;
  }
};

for (const match of matches(obj, arr1))
  console.log(`status: ${match}`);

for (const match of matches(obj, arr2))
  console.log(`status: ${match}`);

for (const match of matches(obj, arr3))
  console.log(`status: ${match}`);

This should work.

Andrea Giammarchi
  • 3,038
  • 15
  • 25
0

Another one solution

const obj = { "active": 12, "inactive": 14, "neutral": 16 }

const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];

const getStatuses= (obj, arr) => arr
  .map(arrValue => {
    const [status] = Object.entries(obj)
      .find(([objKey, objValue]) => objValue === arrValue) 
      ?? [null];
    return { status };
  });

console.log(getStatuses(obj, arr1));
console.log(getStatuses(obj, arr2));
console.log(getStatuses(obj, arr3));
.as-console-wrapper{min-height: 100%!important; top: 0}
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

Get the keys using Object.keys() and then filter them by taking the value from the original object, and checking if it's included in the array:

const getStatus = (obj, arr) => Object.keys(obj)
  .filter(key => arr.includes(obj[key]))
  
const obj = { "active": 12, "inactive": 14, "neutral": 16 }

const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];

console.log(getStatus(obj, arr1));
console.log(getStatus(obj, arr2));
console.log(getStatus(obj, arr3));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209