0

Sample array of objects:

{"29": "DTE Queue", "30": "Services Reporting Sales", "31": "Services Reporting Ops", "41": "UPLOAD", "55": "Support Report"}.

I'm getting input from user as 'sup'. Then output should be {"55": "Support Report"}.

function getKeyByValue(object, value) {
  return Object.keys(object).find((key) => object[key] === value);
}
1
  • 2
    Your "sample array of objects" is one object, not an array of objects. Commented Jan 30, 2023 at 10:17

3 Answers 3

1

You can convert the object to an array of entries, find the entry and convert it back to an object:

const obj = {"29": "DTE Queue", "30": "Services Reporting Sales", "31": "Services Reporting Ops", "41": "UPLOAD", "55": "Support Report"};

function getObjectByValue(object, value) {
  try {
    return Object.fromEntries([Object.entries(object).find(([key, val]) => val.toLowerCase().includes(value.toLowerCase()))]);
  } catch (err) {
    console.error('Object not found');
    return {};
  }
}

console.log(getObjectByValue(obj, 'sup'));
console.log(getObjectByValue(obj, 'sup2'));

Sign up to request clarification or add additional context in comments.

6 Comments

This will give error in Object.fromEntries() if the item is not found for that particular value
Property 'toLowerCase' does not exist on type 'unknown'. I'm getting this error for val.toLowerCase()
Thanks jabaa and ankit. I just did a modification by giving datatype object inside parameter. It worked like a gem :)
@DIMPLEPANDA Property 'toLowerCase' does not exist on type 'unknown'. That's a TypeScript error. You have to type the parameters.
when I search services Im getting 30: services reporting sales but I should also get 31:services reporting ops along with it. Any idea what shd I add?
|
1

const obj = {
  "29": "DTE Queue",
  "30": "Services Reporting Sales",
  "31": "Services Reporting Ops",
  "41": "UPLOAD",
  "55": "Support Report"
};

console.log(getKeyByValue(obj, 'sup'))

function getKeyByValue(obj, value) {
  const matchedEntry = Object.entries(obj).find(entry => entry[1].toLowerCase().match(value.toLowerCase()));

  return matchedEntry && Object.fromEntries([matchedEntry])
}

Comments

0

You can use Object.entries() and Object.fromEntries() for the result you are expecting:

const obj = {
  "29": "DTE Queue",
  "30": "Services Reporting Sales",
  "31": "Services Reporting Ops",
  "41": "UPLOAD",
  "55": "Support Report"
};

function getKeyByValue(object, value) {
  const res = Object.entries(object).find((arr) => arr.includes(value));
  if (res) {
    return Object.fromEntries([res]);
  }
}

console.log(getKeyByValue(obj, 'Support Report'));

3 Comments

if I console res then I'm getting undefined. Also we should convert to lower case and compare right?
Share your code @DIMPLEPANDA
getKeyByValue(obj,value){ console.log(obj,value); console.log(Object.keys(obj)); console.log(Object.values(obj)); try{ return (<any>Object).fromEntries([Object.entries(obj).find(([key, val]) => val.toLowerCase().includes(value.toLowerCase()))]); } catch(err){ console.error('Object not found'); return {} } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.