Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda