最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find a Value in a Dictionary with a certain Property - Stack Overflow

programmeradmin0浏览0评论

Say I have a dictionary like

objDict = {
  id1 : { name: 'someObj1', id: 'someId1' },
  id2 : { name: 'someObj2', id: 'someId2' },
  id3 : { name: 'someObj3', id: 'someId3' },
}

If I wanted to search for the property "someId2" of the "id" property in Values of that dictionary.. how would I be able to grab the the whole object after finding it?

I really can't think of a good way to do it outside of iterating the dictionary with a for-in loop. I was thinking of using Object.values() but even then I can't think of a way of using that to grab the whole object that contains someId2.. I would only be able to determine if that property existed.

I was just wondering if there was a better way than a for-in loop. Thanks

Say I have a dictionary like

objDict = {
  id1 : { name: 'someObj1', id: 'someId1' },
  id2 : { name: 'someObj2', id: 'someId2' },
  id3 : { name: 'someObj3', id: 'someId3' },
}

If I wanted to search for the property "someId2" of the "id" property in Values of that dictionary.. how would I be able to grab the the whole object after finding it?

I really can't think of a good way to do it outside of iterating the dictionary with a for-in loop. I was thinking of using Object.values() but even then I can't think of a way of using that to grab the whole object that contains someId2.. I would only be able to determine if that property existed.

I was just wondering if there was a better way than a for-in loop. Thanks

Share Improve this question asked Sep 23, 2017 at 1:00 user1189352user1189352 3,88513 gold badges59 silver badges99 bronze badges 4
  • Iterate through the dictionary would be the best solution in terms of performance and readability. – Alexander Bell Commented Sep 23, 2017 at 1:03
  • how would I be able to grab the the whole object after finding it? Is this you question? Or you are asking how to search the dict for that value? – jack Commented Sep 23, 2017 at 1:03
  • @jack ideally id love to do something like objDict.find( x => x.id === 'someId2' ) but it's not an array. It's a dictionary. So yes it's a question and I'd like to get the whole object. – user1189352 Commented Sep 23, 2017 at 1:05
  • There are couple alternatives here (stackoverflow./questions/34913675/…) but @Slai is absolutely right: for-in loop will give the best performance (plus, you can break the loop upon the first entry found) – Alexander Bell Commented Sep 23, 2017 at 1:17
Add a ment  | 

4 Answers 4

Reset to default 4

you can just get all the keys using Object.keys(), then iterate over these and select the object you want based on the required id.

var objDict = {
  id1 : { name: 'someObj1', id: 'someId1' },
  id2 : { name: 'someObj2', id: 'someId2' },
  id3 : { name: 'someObj3', id: 'someId3' },
};
var obj;

Object.keys(objDict).forEach(x => obj = objDict[x].id === 'someId2' ? objDict[x]: obj);

console.log(obj);

There is Object.entries(), but for-in loop is more efficient than the other alternatives.

objDict = { id1 : { name: 'someObj1', id: 'someId1' },
            id2 : { name: 'someObj2', id: 'someId2' },
            id3 : { name: 'someObj3', id: 'someId3' } }

item = Object.entries(objDict).find(a => a[1].id === 'someId2')

console.log(JSON.stringify(item))

The following code snippet containing break; statement could statistically provide a better performance vs. the selected one.

objDict = {
  id1 : { name: 'someObj1', id: 'someId1' },
  id2 : { name: 'someObj2', id: 'someId2' },
  id3 : { name: 'someObj3', id: 'someId3' },
};
var obj;
for (var key in objDict)
{
  if (objDict[key].id==='someId2') 
  {  
    obj = objDict [key]; 
    break;
  }
}
console.log(obj);

ECMA Script 2015 provides another alternative of using Map object as described in : How to iterate (keys, values) in javascript?

Hope this may help.

This is a generic indexing problem, so no, there's no good way to do this other than looping through each value in the dictionary. This is because you've created a data structure to access the data quickly (O(1) time), but the drawback is that if you want to find the data through another index quickly, you'll have to search manually O(n) time for it.

If you really did care about the actual value of someId, then the canonical answer would be to create 2 separate dictionaries w/ the same data w/ different key values. This is very similar to having 2 indexes on a database table.

发布评论

评论列表(0)

  1. 暂无评论