简体   繁体   中英

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.

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.

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.

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.

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,
    }
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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