如何使用lodash从复杂的对象数组中获取值?

时间:2017-12-12 08:39:25

标签: javascript arrays typescript lodash

所以我从API中获取这个相当复杂的数组,其中包含大量带有对象的嵌套数组等。它看起来像这样:

    public data: any[] = [
    {
        language: 'Dutch',
        sources: [
            {
                source: 'De Redactie',
                channels: [
                    { channel: 'binnenland', value: false },
                    { channel: 'buitenland', value: false },
                    { channel: 'sport', value: false },
                    { channel: 'cultuur en media', value: false },
                    { channel: 'politiek', value: false },
                    { channel: 'financien', value: false }
                ]
            },
            {
                source: 'Tweakers',
                channels: [
                    { channel: 'hardware', value: false },
                    { channel: 'software', value: false },
                    { channel: 'tech', value: false },
                    { channel: 'smartphones', value: false },
                    { channel: 'audio', value: false },
                    { channel: 'video', value: false }
                ]
            }
        ]
    },
    {
        language: 'English',
        sources: [
            {
                source: 'BBC',
                channels: [
                    { channel: 'news', value: false },
                    { channel: 'sport', value: false },
                    { channel: 'weather', value: false },
                    { channel: 'travel', value: false },
                    { channel: 'politics', value: false }
                ]
            },
            {
                source: 'Fox News',
                channels: [
                    { channel: 'u.s.', value: false },
                    { channel: 'world', value: false },
                    { channel: 'opinion', value: false },
                    { channel: 'politics', value: false },
                    { channel: 'entertainment', value: false },
                    { channel: 'business', value: false }
                ]
            }
        ]
    }
]

我可以使用此函数提取我想要的值(通道属性):

setChannel(channel: string, source: string, language: string) {
    for (const i of this.data) {
        if (i.language === language) {
            for (const j of i.sources) {
                if (j.source === source) {
                    for (const k of j.channels) {
                        if (k.channel === channel) {
                            console.log(k.channel);
                        }
                    }
                }
            }
        }
    }
}

现在我确定lodash中有一个函数来简化这个。因为三重嵌套for和if不是一种很好的编码方式。但我似乎无法在文档中找到它。有人可以指出我正确的方向吗?

2 个答案:

答案 0 :(得分:4)

Lodash的filter()flatMap()会帮助你:

const result = _(data).filter({language: 'Dutch'})
                      .flatMap('sources')
                      .filter({source: 'De Redactie'})
                      .flatMap('channels')
                      .find({channel: 'sport'});



const data = [
{
    language: 'Dutch',
    sources: [
        {
            source: 'De Redactie',
            channels: [
                { channel: 'binnenland', value: false },
                { channel: 'buitenland', value: false },
                { channel: 'sport', value: false },
                { channel: 'cultuur en media', value: false },
                { channel: 'politiek', value: false },
                { channel: 'financien', value: false }
            ]
        },
        {
            source: 'Tweakers',
            channels: [
                { channel: 'hardware', value: false },
                { channel: 'software', value: false },
                { channel: 'tech', value: false },
                { channel: 'smartphones', value: false },
                { channel: 'audio', value: false },
                { channel: 'video', value: false }
            ]
        }
    ]
},
{
    language: 'English',
    sources: [
        {
            source: 'BBC',
            channels: [
                { channel: 'news', value: false },
                { channel: 'sport', value: false },
                { channel: 'weather', value: false },
                { channel: 'travel', value: false },
                { channel: 'politics', value: false }
            ]
        },
        {
            source: 'Fox News',
            channels: [
                { channel: 'u.s.', value: false },
                { channel: 'world', value: false },
                { channel: 'opinion', value: false },
                { channel: 'politics', value: false },
                { channel: 'entertainment', value: false },
                { channel: 'business', value: false }
            ]
        }
    ]
}
]

const result = _(data).filter({language: 'Dutch'})
                      .flatMap('sources')
                      .filter({source: 'De Redactie'})
                      .flatMap('channels')
                      .filter({channel: 'sport'})
                      .first();

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这实际上是一个非常“快速和肮脏”的解决方案,因为我没有使用任何后备默认值。但是,您可以阅读有关 .find .get

const data = [{
    language: 'Dutch',
    sources: [{
        source: 'De Redactie',
        channels: [{
            channel: 'binnenland',
            value: false
          },
          {
            channel: 'buitenland',
            value: false
          },
          {
            channel: 'sport',
            value: false
          },
          {
            channel: 'cultuur en media',
            value: false
          },
          {
            channel: 'politiek',
            value: false
          },
          {
            channel: 'financien',
            value: false
          }
        ]
      },
      {
        source: 'Tweakers',
        channels: [{
            channel: 'hardware',
            value: false
          },
          {
            channel: 'software',
            value: false
          },
          {
            channel: 'tech',
            value: false
          },
          {
            channel: 'smartphones',
            value: false
          },
          {
            channel: 'audio',
            value: false
          },
          {
            channel: 'video',
            value: false
          }
        ]
      }
    ]
  },
  {
    language: 'English',
    sources: [{
        source: 'BBC',
        channels: [{
            channel: 'news',
            value: false
          },
          {
            channel: 'sport',
            value: false
          },
          {
            channel: 'weather',
            value: false
          },
          {
            channel: 'travel',
            value: false
          },
          {
            channel: 'politics',
            value: false
          }
        ]
      },
      {
        source: 'Fox News',
        channels: [{
            channel: 'u.s.',
            value: false
          },
          {
            channel: 'world',
            value: false
          },
          {
            channel: 'opinion',
            value: false
          },
          {
            channel: 'politics',
            value: false
          },
          {
            channel: 'entertainment',
            value: false
          },
          {
            channel: 'business',
            value: false
          }
        ]
      }
    ]
  }
];

const setChannel = (channel = '', source = '', language = '') => {
  // get all sources for the given language
  const sourcesForLanguage = _.get(_.find(data, ['language', language]), 'sources');
  // get all channels for given source
  const channelsForSource = _.get(_.find(sourcesForLanguage, ['source', source]), 'channels');
  // get the channel object for given channel
  const selectedChannel = _.find(channelsForSource, ['channel', channel]);
  console.log(selectedChannel);
}

setChannel('news', 'BBC', 'English');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>