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

searching an id in an array of dictionary in javascript - Stack Overflow

programmeradmin3浏览0评论

I have a array of dictionary in JavaScript and I want to add an element to one of the dictionary in the array by matching if an ID is found. How to search if an ID is found?

This is my dictionary:

{ 
    priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA' } ],
    test: [ { socketid: 'Q2n5RzcJqPeLZ5T9AAAB' } ] } ] 
}

I want to add an element by searching for socketid "bVLmrV8I9JsSyON7AAAA" and add an element to the dictionary.

I have a array of dictionary in JavaScript and I want to add an element to one of the dictionary in the array by matching if an ID is found. How to search if an ID is found?

This is my dictionary:

{ 
    priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA' } ],
    test: [ { socketid: 'Q2n5RzcJqPeLZ5T9AAAB' } ] } ] 
}

I want to add an element by searching for socketid "bVLmrV8I9JsSyON7AAAA" and add an element to the dictionary.

Share Improve this question edited Jul 7, 2014 at 8:18 Sergiu Paraschiv 10.2k5 gold badges40 silver badges50 bronze badges asked Jul 7, 2014 at 8:15 Poorna ChandPoorna Chand 1031 gold badge1 silver badge7 bronze badges 3
  • you want the result object like priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA',word:meaning } ] or some other way – Mritunjay Commented Jul 7, 2014 at 8:20
  • Is the dictionary with socketid property always the first element in the array? – Ja͢ck Commented Jul 7, 2014 at 8:37
  • shouldnt the title of the question be "searching a value in a dictionary of dictionaries"? – ggonmar Commented May 31, 2019 at 6:39
Add a ment  | 

5 Answers 5

Reset to default 5

In the current state of the data you'd have to use a linear search at least.

var keyToFind = 'Q2n5RzcJqPeLZ5T9AAAB';
for(var i in dictionary){
    if(dictionary[i].socketid == keyToFind){
        // Add an element to the dictionary
        break; // If you want to break out of the loop once you've found a match
    }
}

You're question is rather confusing but I think what you're trying to do is :

var myDict = { 
    priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA' } ],
    test    : [ { socketid: 'Q2n5RzcJqPeLZ5T9AAAB' } ],
 }

and then looping through the array (although in this case, they only contain one item...)

var arr = myDict.priyanka
for(var i=0; i<arr.length ; i++){
    if(arr[i].socketid === 'bVLmrV8I9JsSyON7AAAA'){
        var el = /whatever you wanna add?/
        arr.push(el);
       }
}
var myObject = { 
    priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA' } ],
    test: [ { socketid: 'Q2n5RzcJqPeLZ5T9AAAB' } ] } ] 
};

myObject.priyanka.forEach(function(item){
    if(item.socketid === 'bVLmrV8I9JsSyON7AAAA'){
        // There is a socketid equals to bVLmrV8I9JsSyON7AAAA in priyanka
        break;
    } else {
        // Socketid not found
    }
});

Be aware that Array.forEach is not fully cross browser patible, maybe you want to go with a simple for loop :

for(var i=0; i < myObject.priyanka.length; i++){
    if(myObject.priyanka[i].socketid === 'bVLmrV8I9JsSyON7AAAA'){
        // There is a socketid equals to bVLmrV8I9JsSyON7AAAA in priyanka
        break;
    } else {
        // Socketid not found
    }
}

You can add elements like this

Dictionary

var dict = { 
priyanka: [ { socketid: 'bVLmrV8I9JsSyON7AAAA' } ],
test: [ { socketid: 'Q2n5RzcJqPeLZ5T9AAAB' } ] }

function

var addToDict = function (dict,id,element) {
    for(key in dict){   //search in each key of dictionary
       var obj = dict[key];
       if(obj[0].socketid == id)   // if that key contains socketId
         obj.push(element);    // add element to key's array
    }
}

call the function

addToDict(dict,'bVLmrV8I9JsSyON7AAAA',{"word":"meaning"}) 
console.log(dict)  // because object will be passed by reference the `dict` will changed.

You can use this helper function:

function findInDict(dict, predicate) 
{
    for (var prop in dict) {
        if (dict.hasOwnProperty(prop) && predicate(dict[prop], prop, dict)) {
            return prop;
        }
    }
    return null;
  };
}

Then perform the search:

// find property on main dictionary
var prop = findInDict(dict, function(value, name) {
    // name is property name
    // value is property value
    return value[0].socketid == 'bVLmrV8I9JsSyON7AAAA';
});

if (prop === null) {
    // prop doesn't exist, so create one?
}
发布评论

评论列表(0)

  1. 暂无评论