繁体   English   中英

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

[英]Create object array with dynamic properties

在targetData数组中,具有属性的对象(“ january”等)从源数组中获取。...我无法将sourceEvents转换为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,
    }
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM