如何从一个对象中创建两个对象,用lodash

时间:2017-12-14 09:59:50

标签: arrays angular typescript object lodash

考虑这个对象,有两个NL语言通道和一个EN语言通道:

[
    {
        "name": "De Redactie",
        "channels": [
            {
                "name": "headlines",
                "pubDate": "2017-05-15 09:15:00",
                "language": "nl",
                "items": [

                ]
            },
            {
                "name": "headlines English",
                "pubDate": "2017-05-14 18:05:00",
                "language": "en",
                "items": [

                ]
            },
            {
                "name": "politiek",
                "pubDate": "2017-05-14 20:11:00",
                "language": "nl",
                "items": [

                ]
            }
        ]
    }
]

我如何划分它们以便我能得到这个结果:

[
    {
        "name": "De Redactie",
        "channels": [
            {
                "name": "headlines",
                "pubDate": "2017-05-15 09:15:00",
                "language": "nl",
                "items": [

                ]
            },
            {
                "name": "politiek",
                "pubDate": "2017-05-14 20:11:00",
                "language": "nl",
                "items": [

                ]
            }
        ]
    }
    {
        "name": "De Redactie",
        "channels": [
            {
                "name": "headlines English",
                "pubDate": "2017-05-14 18:05:00",
                "language": "en",
                "items": [

                ]
            }
        ]
    }
]

请注意,这是dummyData。实际数据可以包含一种语言的x量和第二种或第三种或第四种的y量......

我试过在lodash文档中查找正确的函数组合。还尝试了各种复杂的forEach结构,但无法绕过它。

最好使用lodash或打字稿的解决方案,因为我在Angular 4中工作。

2 个答案:

答案 0 :(得分:3)

使用Array#map迭代数组。对于每个对象,使用带有destructuringobject rest提取频道数组。使用Array#reduce对频道进行迭代,并使用相同语言将频道分组到Map。通过传播Map's values iterator来转换回数组。

通过映射对象并将组指定为对象的channels prop来创建对象数组。通过传播到Array#concat

来展平数组

const data = [{"name":"De Redactie","channels":[{"name":"headlines","pubDate":"2017-05-15 09:15:00","language":"nl","items":[]},{"name":"headlines English","pubDate":"2017-05-14 18:05:00","language":"en","items":[]},{"name":"politiek","pubDate":"2017-05-14 20:11:00","language":"nl","items":[]}]}];

const result = [].concat(...data.map(({ channels, ...rest }) => {
  const channelGroups = [...channels.reduce((m, channel) => {
    m.has(channel.language) || m.set(channel.language, []);
    m.get(channel.language).push(channel);
  
    return m;
  }, new Map()).values()];
  
  return channelGroups.map((channels) => ({
    ...rest,
    channels
  }));
}));

console.log(result);

答案 1 :(得分:1)

用lodash的方式:

const res = _.chain(arr)
    .flatMap(item => _.map( // get array of channels with parent name
        item.channels,
        channel => _.assign({}, channel, { parentName: item.name })
    ))
    .groupBy('language') // group channels by language
    .values() // get channel arrays for each lang
    .map(langArrs => ({ // set finished structure
        name: _.first(langArrs).parentName, // get name for lang channels
        channels: _.omit(langArrs, ['parentName']) // set channels without parent name
    }))
    .value();