如何在javascript中的数组中创建自定义对象?
JSON:(this.schedulerForm.get(“SchedulerList”).value=>此数组包含以下数组..) 转换JSON:(上面的JSON数组包含1个对象和3个数量值。我需要用相同的公式将每个对象中的数量值与月份和年份分开。输出应如下所示。) 组件1.ts:如何在javascript中的数组中创建自定义对象?,javascript,arrays,Javascript,Arrays,JSON:(this.schedulerForm.get(“SchedulerList”).value=>此数组包含以下数组..) 转换JSON:(上面的JSON数组包含1个对象和3个数量值。我需要用相同的公式将每个对象中的数量值与月份和年份分开。输出应如下所示。) 组件1.ts: month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]; year = new
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
year = new Date().getFullYear();
getDate(n){
const date = new Date();
return date.getMonth() + n;
}
save() {
console.log(this.schedulerForm.get("schedularList").value)
}
我想这样的事情会对你有所帮助:
const数据=[{
公式编号:1,
数量1:10,
数量2:20,
数量3:40
}];
const year=新日期().getFullYear();
常量月=13;
常量mapItem=obj=>propQuantity=>({
公式id:obj.formula\u id,
年,
月,
数量:obj[propQuantity]
});
const props=[“quantity1”、“quantity2”、“quantity3”];
const result=data.reduce((agg,obj)=>[…agg,…props.map(mapItem(obj))],[]);
控制台日志(结果)代码>到目前为止你尝试了什么?这仍然与角度和反应形式无关!谢谢你提供这个解决方案。。如何做这个解决方案的反之亦然??你能告诉我如何做吗???我尝试过使用groupby函数,但没有得到正确的数据。你要求重建原始数组吗?@Rekha很抱歉,我准备了一个JSFIDLE,其中包含了你要求的代码:如果这符合你的需要,请告诉我
const result = [{
formula_id:1,
year:this.year,
month: this.month,
quantity:10
},
{
formula_id:1,
year:this.year,
month: this.month,
quantity:20
},
{
formula_id:1,
year:this.year,
month: this.month,
quantity:40
}]
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
year = new Date().getFullYear();
getDate(n){
const date = new Date();
return date.getMonth() + n;
}
save() {
console.log(this.schedulerForm.get("schedularList").value)
}