How to check if value exists in this JavaScript array?

后端 未结 5 1662
南旧
南旧 2021-01-02 00:43

I have a JavaScript array, where each new item added to the array gets the next incremental number. An example would be as follows (I hope Im writing this correctly):

<
5条回答
  •  既然无缘
    2021-01-02 01:16

    ArrayofPeople = new Array();
    ArrayofPeople[0] = [{"id": "529", "name": "Bob"}];
    ArrayofPeople[1] = [{"id": "820", "name": "Dave"}];
    ArrayofPeople[2] = [{"id": "235", "name": "John"}];
    
    var str = '820';
    var is_found = 'not found';
    for(item in ArrayofPeople){
        target = ArrayofPeople[item][0];
        if(target['id'] === str)
            is_found = 'found';
    }
    alert(is_found);
    

提交回复
热议问题