searching an id in an array of dictionary in javascript

arrays, dictionary, javascript

Solution

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
    }
}

Problem

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.

Original source