使用动态属性创建对象数组

时间:2016-02-25 11:04:26

标签: javascript html arrays angularjs-ng-repeat

在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来循环生成的数组。

2 个答案:

答案 0 :(得分:0)

很难绕过你所展示的结构。我建议改为:

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:一个月,然后是当月条目的下一个。

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



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>";
&#13;
&#13;
&#13;

注意:您的源数据有一些错误,我在上面已经修复过。

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

答案 1 :(得分:0)

注意引号或缺少引号:

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