操纵javascript对象数组以获得一组新的对象数组

操纵javascript对象数组以获得一组新的对象数组,javascript,arrays,object,Javascript,Arrays,Object,我有这个对象数组 const unitsAndRates = [ { propertyUnitType: "store", propertyUnitRate: "20000", numberOfUnit: 4 }, { propertyUnitType: "duplex", propertyUnitRate: "20000", numberOfUnit: 3

我有这个对象数组

const unitsAndRates = [
  {
    propertyUnitType: "store",
    propertyUnitRate: "20000",
    numberOfUnit: 4
  },
  {
    propertyUnitType: "duplex",
    propertyUnitRate: "20000",
    numberOfUnit: 3
  }
];

我需要类似于上面生成的数组的东西

const generated = [
  {
    propertyUnitType: "store1",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "store2",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "store3",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "store4",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "duplex1",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "duplex2",
    propertyUnitRate: "20000"
  },
  {
    propertyUnitType: "duplex3",
    propertyUnitRate: "20000"
  }
];

考虑到
numberOfUnit
,如何生成上述输出?

这可以使用
Array.prototype.flatMap
完成

const unitsAndRates=[
{
propertyUnitType:“存储”,
地产硝酸盐:“20000”,
人数单位:4
},
{
propertyUnitType:“双面打印”,
地产硝酸盐:“20000”,
人数单位:3
}
];
const result=unitsAndRates.flatMap((项)=>[…数组(项.numberOfUnit.keys()])。map((索引)=>({
propertyUnitType:item.propertyUnitType+(索引+1),
propertyUnitRate:item.propertyUnitRate
})));

控制台日志(结果)这可以使用
Array.prototype.flatMap
完成

const unitsAndRates=[
{
propertyUnitType:“存储”,
地产硝酸盐:“20000”,
人数单位:4
},
{
propertyUnitType:“双面打印”,
地产硝酸盐:“20000”,
人数单位:3
}
];
const result=unitsAndRates.flatMap((项)=>[…数组(项.numberOfUnit.keys()])。map((索引)=>({
propertyUnitType:item.propertyUnitType+(索引+1),
propertyUnitRate:item.propertyUnitRate
})));
控制台日志(结果)

如果需要按numberOfUnit字段进行筛选,请尝试:

const filteredUnits = unitsAndRates.filter(item => item.numberOfUnit === "2000") 
然后你可以做映射

filteredUnits.map({propertyUnitType, propertyUnitRate} => ({propertyUnitType, propertyUnitRate }))

如果需要按numberOfUnit字段进行筛选,请尝试:

const filteredUnits = unitsAndRates.filter(item => item.numberOfUnit === "2000") 
然后你可以做映射

filteredUnits.map({propertyUnitType, propertyUnitRate} => ({propertyUnitType, propertyUnitRate }))

复制的规则是什么?你尝试了什么?复制的规则是什么?你尝试了什么?非常感谢@Derek。成功了。非常感谢@Derek。工作。