Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
how to get the object key if the object and array value matches in javascript

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

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}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!