简体   繁体   English

在javascript中搜索字典数组中的id

[英]searching an id in an array of dictionary in javascript

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. 我在JavaScript中有一个字典数组,我想通过匹配找到一个ID,在数组中的一个字典中添加一个元素。 How to search if an ID is found? 如何搜索是否找到了ID?

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. 我想通过搜索socketid “bVLmrV8I9JsSyON7AAAA”添加一个元素,并在字典中添加一个元素。

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
    }
}
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 compatible, maybe you want to go with a simple for loop : 请注意,Array.forEach不是完全跨浏览器兼容的,也许您想要使用简单的for循环:

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'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);
       }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM