How to find an appropriate object in array by one of its properties

后端 未结 3 1730
后悔当初
后悔当初 2020-12-04 04:11

Say, I have that simple array of objects.

var x = [
            {
                id:      1001
                name:    \"Jim\",
                surname: \"         


        
3条回答
  •  不知归路
    2020-12-04 04:23

    A simple loop will suffice:

    for(var i=0, len=x.length; i < len; i++){
        if( x[i].id === 1002){
            //do something with x[i]
        }
    }
    

    Even though you are dealing with json remember that it's all just a hierarchy of arrays, objects and primitives.

提交回复
热议问题