我有一些对象,我想通过数组循环并将对象映射到局部变量
数组对象
[
{startDate: "05/01", endDate: "05/07", label: "05/01 - 05/07 (week1)"},
{startDate: "05/08", endDate: "05/14", label: "05/08 - 05/14 (week2)"},
{startDate: "05/15", endDate: "05/15", label: "05/15 - 05/15 (week3)"}
]
现在我想只带标签对象并将其推送到this.dateLable
预期
this.dateLable =[
{ id: '05/01 - 05/07', label: "05/01 - 05/07 (week1)" },
{ id: '05/08 - 05/14', label: "05/08 - 05/14 (week2)" },
{ id: '05/15 - 05/15', label: "05/15 - 05/15 (week3)"}
]
我的代码
this.weekByDates().find(x => x.label == push(this.dateLable) )
请帮我说明如何实现这个目标?
答案 0 :(得分:1)
假设std::thread
是初始对象数组:
arr
答案 1 :(得分:0)
您可以使用Array.map
转换数组中的数据:
let source = [{startDate: "05/01", endDate: "05/07", label: "05/01 - 05/07 (week1)"},{startDate: "05/08", endDate: "05/14", label: "05/08 - 05/14 (week2)"},{startDate: "05/15", endDate: "05/15", label: "05/15 - 05/15 (week3)"}];
let dateLable = source.map(v=> ({
id: `${v.startDate} - ${v.endDate}`, label: v.label
}));
console.log(dateLable);
答案 2 :(得分:0)
您可以尝试以下
var arr = [
{startDate: "05/01", endDate: "05/07", label: "05/01 - 05/07 (week1)"},
{startDate: "05/08", endDate: "05/14", label: "05/08 - 05/14 (week2)"},
{startDate: "05/15", endDate: "05/15", label: "05/15 - 05/15 (week3)"}
]
this.dateLabel =arr.map(function(item){
return {
id : item.startDate + " - " + item.endDate,
label: item.label
};
});
console.log(this.dateLabel);