降低多维数组的维数

3

我目前正在尝试映射和减少函数以展平多维数组。
这是一个模拟示例数据集:

data: [
  {
    label: "Sort-01"
    data: [
      {
        label: "OCT-2017"
        weight: 2304
      },
      {
        label: "NOV-2017"
        weight: 1783
      }
    ]
  },
  {
    label: "Sort-02"
    data: [
      {
        label: "OCT-2017"
        weight: 4785
      },
      {
        label: "NOV-2017"
        weight: 102
      }
    ]
  },
 ......
]

我知道为了按排序号进行MapReduce,我可以使用以下方法:
data.map(sort => sort.data.reduce((a,b) => a.weight + b.weight));

然而,我想按月减少而不是排序号码。
任何帮助都将不胜感激。

谢谢,
杰伊

2个回答

2
使用Array#map获取data属性数组的数组,然后使用Array#concatspread将其展开。使用Array#reduce将值收集到Map中,然后使用Map#values和展开语法将其转换回数组:

const array = [{"label":"Sort-01","data":[{"label":"OCT-2017","weight":2304},{"label":"NOV-2017","weight":1783}]},{"label":"Sort-02","data":[{"label":"OCT-2017","weight":4785},{"label":"NOV-2017","weight":102}]}];

const result = [... // spread the values iterator to an array
  [].concat(...array.map(({ data }) => data)) // map the array into an array of data arrays
  .reduce((m, { label, weight }) => {
    // take item if label exists in map, if not create new
    const item = m.get(label) || { label, weight: 0 };
    
    item.weight += weight; // add the weight
  
    return m.set(label, item); // set the item in the map
  }, new Map).values()] // get the values iterator

console.log(result);

这里是无间距版本:

const array = [{"label":"Sort-01","data":[{"label":"OCT-2017","weight":2304},{"label":"NOV-2017","weight":1783}]},{"label":"Sort-02","data":[{"label":"OCT-2017","weight":4785},{"label":"NOV-2017","weight":102}]}];

const helper = Object.create(null);
const result = [].concat.apply([], array.map(({ data }) => data)) // map the array into an array of data arrays, and flatten by apply concat
  .reduce((r, { label, weight }) => {
    // if label is not in helper, add to helper, and push to r
    if(!helper[label]) {
      helper[label] = { label, weight: 0 };
      r.push(helper[label]);
    }
    
    helper[label].weight += weight; // add the weight to the object
  
    return r;
  }, []) // get the values iterator

console.log(result);


有没有不使用spread操作符的方法来实现这个?我认为我目前正在工作的项目还没有使用spread操作符。我遇到了一个“[].concat.apply(...).reduce(...).values(...).slice is not a function”的错误。 - Jay

1
你可以使用reduce或concat(参见Ori的答案)来获取扁平化的数据数组。从那里重新分配你的年份标签作为键,重量作为值。 然后传递给另一个reducer以按月总计你的重量。

const data = [{label: "Sort-01",data: [{label: "OCT-2017",weight: 2304,},{label: "NOV-2017",weight: 1783,},],},{label: "Sort-02",data: [{label: "OCT-2017",weight: 4785,},{label: "NOV-2017",weight: 102,},],},];
const flatten = (acc, cur) => { 
  cur.data.forEach(val => acc.push(val)); 
  return acc;
};
const monthMap = ({ label, weight }) => ({ [label]: weight });
const reducer = (acc, cur) => {
  const key = Object.keys(cur)[0]
  if (!acc.hasOwnProperty(key)) { acc[key] = 0 }
  acc[key] += cur[key];
  return acc;
};

let x = data.reduce(flatten, []).map(monthMap).reduce(reducer, {});
console.log(x);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接