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

215
Views
How to get the parent object value using child value

I have value "b" with me and I want to get the item "3648" using that how can I do that using JavaScript?

{
  "someID": "16385421",
  "items": {
    "9836": {
      "id": "a"
    },
    "3648": {
      "id": "b"
    },
    "7738": {
      "id": "c"
    }
  }
}

o/p

"3648": {
  "id": "b"
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Edited after your comment:

getByItemId = ( val ) => {
  let result;
  for( let elem in data.items ){
    const check = data.items[ elem ];
    if( check.id == val ) result = check;
  }
  console.log( result )
  return result;
}
getByItemId( "b" )
about 4 years ago · Juan Pablo Isaza Report

0

You could do it with some code that resembled the following.

First we get all of the object keys in the obj.items object. Next we iterate through them. At each step, we check to see if the desired ID is contained within the sub-object indexed by the current key in the array. If it's found, we update the result object. We then return an empty object if the key isn't present, and something like the following if it was found: {key: '3648', id: 'b'}.

"use strict";
function byId(id){return document.getElementById(id)}
function qs(sel,parent=document){return parent.querySelector(sel)}
function qsa(sel,parent=document){return parent.querySelectorAll(sel)}
window.addEventListener('load', onLoaded, false);

var rawData = {
  "someID": "16385421",
  "items": {
    "9836": {
      "id": "a"
    },
    "3648": {
      "id": "b"
    },
    "7738": {
      "id": "c"
    }
  }
};

function getIDs(data, searchItemId)
{
    let keys = Object.keys(data.items);
    let result = {};
    keys.forEach( keyFunc );
    return result;
    
    function keyFunc(elem, index, collection)
    {
        if (data.items[elem].id == searchItemId)
        {
            result.key = elem;
            result.id = searchItemId;
        }
    }
}

function onLoaded(evt)
{
    console.log( getIDs(rawData, "b") );
}

about 4 years ago · Juan Pablo Isaza Report

0

You can do:

const obj = {
  someID: '16385421',
  items: {
    9836: {
      id: 'a',
    },
    3648: {
      id: 'b',
    },
    7738: {
      id: 'c',
    },
  },
}

const getParentObjectByValue = (obj, value) => {
  const key = Object.keys(obj.items).find((k) => obj.items[k].id === value)
  return key ? { [key]: obj.items[key].id } : undefined
}

console.log(getParentObjectByValue(obj, 'b'))
console.log(getParentObjectByValue(obj, 'zz'))

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!