繁体   English   中英

迭代 Typescript 中的分组对象

[英]Iterate over grouped object in Typescript

我有一个对象,其中包含通过 lodash groupBy 函数映射的分组数据,迭代集合并检索以下信息的最佳方法是什么:

  • 总产品数,
  • totalProductSum = Sum(productsNumberInGroup * price)
  • 每组产品数量

所以输出可能是

let result = {
totalProductNumber: 8,
totalProductSum: 17,
productsNumberPerGroup: [{1:2}, {2:3}, {3:3}] // or sth. like this
}
interface IProduct {
  name: string;
  color: string;
  price: string;
 id: number
 }

    const products = {
  "1": [{
      "name": "Jim",
      "color": "blue",
      "price": 1,
      "id": 1
    },
    {
      "name": "Jim",
      "color": "blue",
      "price": 1,
      "id": 1
    }
  ],
  "2": [{
      "name": "Eddie",
      "color": "green",
      "price": 2,
      "id": 2
    },
    {
      "name": "Eddie",
      "color": "green",
      "price": 2,
      "id": 2
    },
    {
      "name": "Eddie",
      "color": "green",
      "price": 2,
      "id": 2
    }
  ],
  "3": [{
      "name": "Ela",
      "color": "pink",
      "price": 3,
      "id": 3
    },
    {
      "name": "Ela",
      "color": "pink",
      "price": 3,
      "id": 3
    },
    {
      "name": "Ela",
      "color": "pink",
      "price": 3,
      "id": 3
    }
  ]
}

我的假设是使用 Object.entries 或 Object.values

  let totalSum: number;
  let totalPrices: number[];
Object.values(products)
      .map((prod) => {
        (prod as IProduct[]).map((p) => {
            this.totalPrices.push(Number(p.price)); // here getting Cannot read property 'push' of undefined
        });
      });

你应该做两个改变

let totalPrices: number[];

let totalPrices: number[] = [];

界面的另一变化

interface IProduct {
  name: string;
  color: string;
  price: string;
  id: number
}

改变

price: string;

price: number;

具有可变风格

interface IProduct {
  name: string
  color: string
  price: number
  id: number
}

type Products = Record<string, IProduct[]>

const products: Products = { ... }

let totalProductNumber = 0
let totalProductSum = 0
let productsNumberPerGroup: [string, number][] = []

Object.entries(products).map(([grpId, grpProducts]) => {
  totalProductNumber = totalProductNumber + grpProducts.length
  totalProductSum = totalProductSum + grpProducts.reduce((acc, curr) => acc + curr.price, 0)
  productsNumberPerGroup = [...productsNumberPerGroup, [grpId, grpProducts.length]]
})

console.log(totalProductNumber) // 8
console.log(totalProductSum) // 17
console.log(productsNumberPerGroup) //[ [ '1', 2 ], [ '2', 3 ], [ '3', 3 ] ]

风格不变


type Result = [
  totalProductNumber: number,
  totalProductSum: number,
  productsNumberPerGroup: [string, number][]
]
const initial: Result = [0, 0, []]

const [totalNum, totalSum, prdPerGrp] = Object.entries(products).reduce<Result>(
  ([num, sum, perGrp], [grpId, grpProducts]) => {
    return [
      num + grpProducts.length,
      sum + grpProducts.reduce((acc, curr) => acc + curr.price, 0),
      [...perGrp, [grpId, grpProducts.length]]
    ]
  },
  initial
)

console.log(totalNum) // 8
console.log(totalSum) // 17
console.log(prdPerGrp) // [ [ '1', 2 ], [ '2', 3 ], [ '3', 3 ] ]

暂无
暂无

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

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