简体   繁体   English

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

[英]Create object array with dynamic properties

In the targetData array, objects has properties("january",etc ) are taken from source array.... I am unable to transform sourceEvents to TargetData array. 在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"

        }]
}
]

I need to use ng-repeat to loop the resulting array. 我需要使用ng-repeat循环生成的数组。

It'll be hard to loop through the structure you've shown. 遍历显示的结构将很困难。 I'd suggest this instead: 我建议这样做:

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"

            }
        ]
    }
];

That way, you have nested ng-repeat s: One for months, then the next for the entries in the month. 这样,您就嵌套了ng-repeat s:一个月,然后一个月。

In which case, it's fairly easy to produce with a loop over your source data and a temporary index of months to entries: 在这种情况下,使用源数据循环和条目的临时月份索引是很容易产生的:

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

Note: Your source data had a couple of typos which I've fixed in the above. 注意:您的源数据有一些错字,我已经在上面修正了。


Side note: I've used targetData rather than TargetData in the above because it's more in keeping with the majority of JavaScript style guides. 旁注:上面我使用了targetData而不是TargetData ,因为它与大多数JavaScript样式指南更加一致。

Note the quotes or lack thereof: 请注意引号或缺少引号:

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