简体   繁体   English

实现以下结果的最佳方法是什么?

[英]What is the best way to achieve the following result?

I have the following data:我有以下数据:

const data = [
  {
    id: 1,
    metadata: {
      attributes: [
        {
          type: 'background',
          value: 'red',
        },
        {
          type: 'background',
          value: 'blue',
        },
        {
          type: 'size',
          value: 'small',
        },
      ],
    },
  },
  {
    id: 2,
    metadata: {
      attributes: [
        {
          type: 'background',
          value: 'red',
        },
        {
          type: 'background',
          value: 'blue',
        },
      ],
    },
  },
  {
    id: 3,
    metadata: {
      attributes: [
        {
          type: 'background',
          value: 'red',
        },
        {
          type: 'background',
          value: 'green',
        },
        {
          type: 'size',
          value: 'small',
        },
      ],
    },
  },
];

For each object in the attributes array I have to create a new object based on the type property.对于属性数组中的每个对象,我必须基于 type 属性创建一个新对象。 The type property will be a key in this new object and the value will be another property nested inside. type 属性将是这个新对象中的键,值将是嵌套在其中的另一个属性。 The value of the nested property will be an array that contains all the corresponding ids.嵌套属性的值将是一个包含所有相应 id 的数组。 So, I have to achieve something like this as a final result:所以,我必须实现这样的最终结果:

const desiredResult = {
    background: {
      red: [1, 2, 3], //these are ids
      blue: [1, 2],
      green: [3],
    },
    size: {
      small: [1, 3],
    },
};

You can try for each or for ... of in javascript.您可以在 javascript 中尝试for eachfor ... of clean code for beginner like below:初学者的干净代码如下:

function process (data) {
    let result = {};
    for (let datum of data) {
        for (let attribute of datum.metadata.attributes) {
            result[attribute.type] = result[attribute.type] || {};
            result[attribute.type][attribute.value] = result[attribute.type][attribute.value] || [];
            result[attribute.type][attribute.value].push(datum.id);
        }
    }
    return result;
}
//
const desiredResult = process(data);

Another approach much like the others here, but using immutable data, even for the accumulator:另一种方法与此处的其他方法非常相似,但使用不可变数据,即使对于累加器也是如此:

 const extract = (xs) => xs .reduce ((a, {id, metadata: {attributes = []} = {}}) => attributes .reduce ((a, {type: t, value: v}) => ({...a, [t]: {... (a [t] || {}), [v]: [...((a [t] || {}) [v] || []), id]}}), a), {}) const data = [{id: 1, metadata: {attributes: [{type: "background", value: "red"}, {type: "background", value: "blue"}, {type: "size", value: "small"}]}}, {id: 2, metadata: {attributes: [{type: "background", value: "red"}, {type: "background", value: "blue"}]}}, {id: 3, metadata: {attributes: [{type: "background", value: "red"}, {type: "background", value: "green"}, {type: "size", value: "small"}]}}] console .log (extract (data))
 .as-console-wrapper {max-height: 100% !important; top: 0}

This works for many sizes of data, but it can run into a performance problem if the data is really large.这适用于许多大小的数据,但如果数据非常大,它可能会遇到性能问题 If you do, then you might have to choose a mutable accumulator instead.如果你这样做了,那么你可能不得不选择一个可变的累加器。 But I wouldn't worry about it unless this function shows itself to be a bottleneck in your application.但是我不会担心它,除非这个函数表明它本身是你应用程序的一个瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM