Find by key deep in a nested array

后端 未结 17 1498
礼貌的吻别
礼貌的吻别 2020-11-22 15:41

Let\'s say I have an object:

[
    {
        \'title\': \"some title\"
        \'channel_id\':\'123we\'
        \'options\': [
                    {
                 


        
17条回答
  •  Happy的楠姐
    2020-11-22 15:43

    If you want to get the first element whose id is 1 while object is being searched, you can use this function:

    function customFilter(object){
        if(object.hasOwnProperty('id') && object["id"] == 1)
            return object;
    
        for(var i=0; i

    If you want to get all elements whose id is 1, then (all elements whose id is 1 are stored in result as you see):

    function customFilter(object, result){
        if(object.hasOwnProperty('id') && object.id == 1)
            result.push(object);
    
        for(var i=0; i

提交回复
热议问题