将数组对象映射到另一个变量

时间:2018-04-18 06:26:39

标签: javascript typescript

我有一些对象,我想通过数组循环并将对象映射到局部变量

数组对象

[
    {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) )

请帮我说明如何实现这个目标?

3 个答案:

答案 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)

使用Array.map

您可以尝试以下



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);