创建具有动态属性的对象数组

Create object array with dynamic properties

本文关键字:对象 数组 属性 动态 创建      更新时间:2023-09-26

在targetData数组中,对象具有属性("一月"等)取自源数组....我无法将源事件转换为目标数据数组。

var sourceEvents =[
    {
        "title": "Course Tile1",
        "details": "Webex| Advanced",
        "month": "January"
    },
    {
        "title": "Course Tile1",
        "details": "Webex| Advnced",
        "month": "february"
    },
    {
        "title": "Course Tile1",
        "details": "Webex| Advnced",
        "month": "febrary"
    }];

var TargetData =[
    {"january":
  [
        {
            "title": "Course Tile1",
            "details": "Webex| Advanced"
        },
        {
            "title": "Course Tile2",
            "details": "Webex| Advanced",
        }
  ]
    },
    { "Feb":[
    {
            "title": "Course Tile3",
            "details": "Webex| Advanced"
        },
        {
            "title": "Course Tile4",
            "details": "Webex| Advanced"
        }]
}
]

我需要使用 ng-repeat 来循环生成的数组。

很难

遍历您显示的结构。我会建议这样做:

var targetData = [
    {
        "month": "january",
        "entries": [
            {
                "title": "Course Tile1",
                "details": "Webex| Advanced"
            }, {
                "title": "Course Tile2",
                "details": "Webex| Advanced",
            }
        ]
    },
    {
        "month": "february",
        "entries": [
            {
                "title": "Course Tile3",
                "details": "Webex| Advanced"
            },
            {
                "title": "Course Tile4",
                "details": "Webex| Advanced"
            }
        ]
    }
];

这样,您就嵌套了ng-repeat s:一个嵌套几个月,下一个嵌套一个月中的条目。

在这种情况下,通过循环源数据和条目月份的临时索引来生成相当容易:

var sourceEvents = [
    {
        "title": "Course Tile1",
        "details": "Webex| Advanced",
        "month": "January"
    },
    {
        "title": "Course Tile1",
        "details": "Webex| Advnced",
        "month": "February"
    },
    {
        "title": "Course Tile1",
        "details": "Webex| Advnced",
        "month": "February"
    }
];
var months = Object.create(null);
var targetData = [];
sourceEvents.forEach(function(event) {
    var month = event.month;
    var entry = months[month];
    if (!entry) {
        months[month] = entry = {
            month: month,
            entries: []
        };
        targetData.push(entry);
    }
    entry.entries.push({
        title: event.title,
        details: event.details
    });
});
document.body.innerHTML =
    "<pre>" + JSON.stringify(targetData, null, 4).replace(/</g, "&lt;") + "</pre>";

注意:您的源数据有几个拼写错误,我已经在上面修复了这些错别字。


旁注:我在上面使用了targetData而不是TargetData,因为它更符合大多数 JavaScript 风格指南。

注意引号或缺少引号:

var sourceEvents = [ 
    {
        title:"Course Tile1",
        details:"Webex| Advanced",
        month:january
    },
    {
        title:"Course Tile1",
        details:"Webex| Advnced",
        month:february
    },
    {
        title:"Course Tile1",
        details:"Webex| Advnced",
        month:february
    }
];
var january = [
    {
        title:"Course Tile1",
        details:"Webex| Advanced"
    },
];
var february = [
    {
        title:"Course Tile1", 
        details:"Webex| Advanced"
    },
];
var targetEvents = [
    {
        january, february,
    }
];