1

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"
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). `"3648": { "id": "b" }` isn’t an output. – Sebastian Simon Dec 07 '21 at 09:50
  • Thanks for the suggestions but sadly this is how I am getting responses from API. in my case "items": {} is an object if its an array it would be easy to filter – Sachin Sakpal Dec 07 '21 at 09:53

3 Answers3

1

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" )
Juergen Riemer
  • 1,393
  • 2
  • 13
  • 29
  • 1
    I don't have an item ID the only value which I have is "b" which is present inside the item. Also, *items* in my case is an *object* – Sachin Sakpal Dec 07 '21 at 10:02
  • @SachinSakpal [You can use bracket notation to access object properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors). But nevertheless this indeed doesn't answer your question. – Ivar Dec 07 '21 at 10:05
  • @SachinSakpal then maybe you should rethink how you've structured your data. That's a bad design. – Joshua Dec 07 '21 at 10:05
  • @Joshua OP already addressed that in their comment under the question. – Ivar Dec 07 '21 at 10:06
  • @Ivar it's not just about array or object access. He just has a value and wants to magically find the parent item that has that value. He doesn't have the key of the child item – Joshua Dec 07 '21 at 10:08
  • I agree its a bad design but this is what I am receiving from the API and I can not modify the API as I am at consumer end – Sachin Sakpal Dec 07 '21 at 10:09
  • @SachinSakpal then communicate with who ever designed the API. There's no magic you can do. What if multiple items have such key or value – Joshua Dec 07 '21 at 10:10
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") );
}
enhzflep
  • 12,927
  • 2
  • 32
  • 51
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'))
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46