Find by key deep in a nested array

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

Let\'s say I have an object:

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


        
17条回答
  •  天命终不由人
    2020-11-22 16:06

    Some time ago I have made a small lib find-and, which is available on npm, for working with nested objects in a lodash manner. There's the returnFound function which returns the found object, or an object array if there's more than one object found.

    E.g.,

    const findAnd = require('find-and');
    
    const a = [
      {
        'title': "some title",
        'channel_id':'123we',
        'options': [
          {
            'channel_id':'abc',
            'image':'http://asdasd.com/all-inclusive-block-img.jpg',
            'title':'All-Inclusive',
            'options':[
              {
                'channel_id':'dsa2',
                'title':'Some Recommends',
                'options':[
                  {
                    'image':'http://www.asdasd.com',
                    'title':'Sandals',
                    'id':'1',
                    'content':{},
                  },
                ],
              },
            ],
          },
        ],
      },
    ];
    
    findAnd.returnFound(a, {id: '1'});
    

    returns

    {
      'image':'http://www.asdasd.com',
      'title':'Sandals',
      'id':'1',
      'content':{},
    }
    

提交回复
热议问题