如何使用 ._groupBy 格式化复杂的数组

How to format complicated array with ._groupBy

我的数组看起来像,但包含 1000 多个对象:

    data = {
  0: {
    code: '3019476',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e1',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'dsfs' },
      { type: 2, label: 'thisOne' },
      { type: 3, label: 'sdas' },
    ],
  },
  1: {
    code: '3019475',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e0',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'sdfsd' },
      { type: 2, label: 'anotherOne' },
      { type: 3, label: 'sdfsd' },
    ],
  },
  2: {
    code: '3019474',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e9',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'gregf' },
      { type: 2, label: 'thisOne' },
      { type: 3, label: 'gregf' },
    ],
  },
};

我希望这是输出:

0: {title: "thisOne", data: Array(2)"}
1: {title: "anotherOne", data: Array(1)"}

所以我想过滤并计算有多少对象包含不同的标签[1].label,将完整的项目数据保存在'data' inside result.

我所有的想法都失败了,所以我真的需要你的帮助

数据中有很多语法错误,修复它们应该可以工作。请参阅下面的代码片段。

注意:对象中的Key/Value对应该用逗号分隔,dsfsthisOnesdas应该是有效的变量或字符串。

data = {
  0: {
    code: '3019476',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e1',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'dsfs' },
      { type: 2, label: 'thisOne' },
      { type: 3, label: 'sdas' },
    ],
  },
  1: {
    code: '3019475',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e0',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'sdfsd' },
      { type: 2, label: 'anotherOne' },
      { type: 3, label: 'sdfsd' },
    ],
  },
  2: {
    code: '3019474',
    _id: '60033f61-8a4e-4622-9731-decd07bc44e9',
    vendor: 'DKNY',
    tags: [
      { type: 1, label: 'gregf' },
      { type: 2, label: 'thisOne' },
      { type: 3, label: 'gregf' },
    ],
  },
};

console.log(data[0].tags[1].label)

你不需要 lodash。您可以使用普通 javascript.

const data= [
{code: "3019476", _id: "60033f61-8a4e-4622-9731-decd07bc44e1", vendor: "DKNY", tags: [{type: 1, label: 'dsfs'},{type: 2, label: 'thisOne'},{type: 3, label: 'sdas'}]}, {code: "3019475", _id: "60033f61-8a4e-4622-9731-decd07bc44e0", vendor: "DKNY", tags: [{type: 1, label: 'sdfsd'},{type: 2, label: 'anotherOne'},{type: 3, label: 'sdfsd'}]}, {code: "3019474", _id: "60033f61-8a4e-4622-9731-decd07bc44e9", vendor: "DKNY", tags: [{type: 1, label: 'gregf'},{type: 2, label: 'thisOne'},{type: 3, label: 'gregf'}]}]

const newData = data.map(d => { 
  let result = {};
  result.title = d.tags.find(tag => tag.type === 2).label;
  result.data = d.tags.filter(tag => tag.type === 2); 
  return result;
});

console.log(newData);

P.S.: 这个片段考虑到可以将多个 tagType 包含到数据数组中,并且第一个找到的将用作标题。 如果那不是你想要的,那么问题应该更清楚。

我怀疑你在找groupBy with a property path iteratee shorthand:

import { groupBy } from 'underscore';

groupBy(data, ['tags', 1, 'label']);
// { thisOne: [Object, Object],
//   anotherOne: [Object]
// }

这会生成一个对象,其中标签作为键,组作为值。从这里,可以很容易地得到问题示例输出的确切形状,例如使用 chain and map:

import { chain } from 'underscore';

chain(data)
    .groupBy(['tags', 1, 'label'])
    .map((data, title) => ({data, title}))
    .value();
// [ { title: 'thisOne', data: [Object, Object] },
//   { title: 'anotherOne', data: [Object] }
// ]