Find by key deep in a nested array

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

Let\'s say I have an object:

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


        
相关标签:
17条回答
  • 2020-11-22 16:00

    If you're already using Underscore, use _.find()

    _.find(yourList, function (item) {
        return item.id === 1;
    });
    
    0 讨论(0)
  • 2020-11-22 16:02

    Just use recursive function.
    See example below:

    const data = [
      {
        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: {},
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
    
    function _find(collection, key, value) {
      for (const o of collection) {
        for (const [k, v] of Object.entries(o)) {
          if (k === key && v === value) {
            return o
          }
          if (Array.isArray(v)) {
            const _o = _find(v, key, value)
            if (_o) {
              return _o
            }
          }
        }
      }
    }
    
    console.log(_find(data, 'channel_id', 'dsa2'))

    0 讨论(0)
  • I've created library for this purpose: https://github.com/dominik791/obj-traverse

    You can use findFirst() method like this:

    var foundObject = findFirst(rootObject, 'options', { 'id': '1' });
    

    And now foundObject variable stores a reference to the object that you're looking for.

    0 讨论(0)
  • 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':{},
    }
    
    0 讨论(0)
  • 2020-11-22 16:07

        fucntion getPath(obj, path, index = 0) {
            const nestedKeys = path.split('.')
            const selectedKey = nestedKeys[index]
    
            if (index === nestedKeys.length - 1) {
                return obj[selectedKey]
            }
    
            if (!obj.hasOwnProperty(selectedKey)) {
                return {}
            }
    
            const nextObj = obj[selectedKey]
    
            return Utils.hasPath(nextObj, path, index + 1)
        }

    You're welcome By: Gorillaz

    0 讨论(0)
提交回复
热议问题