简体   繁体   English

AngularJS - 如何为数组中的每个对象添加新属性

[英]AngularJS - How to add new property to each object in array

I would like to learn how to push a scope variable into each item of an array.我想学习如何将范围变量推送到数组的每个项目中。

This is what I have:这就是我所拥有的:

$scope.ProcessExcel = function (data) {

        //Read the Excel File data.
        var workbook = XLSX.read(data, {
            type: 'binary'
        });

        //Fetch the name of First Sheet.
        var firstSheet = workbook.SheetNames[0];

        //Read all rows from First Sheet into an JSON array.
        var excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);

        //Display the data from Excel file in Table.
        $scope.$apply(function () {
            $scope.AgedaItems = excelRows;
        });
};

The array is $scope.AgendaItems.该数组是 $scope.AgendaItems。 It gets populated by excel data.它由excel数据填充。

It contains:它包含了:

当前数组

I have to push the value of a variable named: $scope.meetingId, to each record so that after pushing it the array contents are:我必须将名为 $scope.meetingId 的变量的值推送到每个记录,以便在推送后数组内容为:

$scope.AgedaItems: Array(5)
0: {MeetingId: "49490", AgendaItem: "1", LegistarID: "61613", Title: "Title#1", __rowNum__: 1}
1: {MeetingId: "49490", AgendaItem: "2", LegistarID: "60826", Title: "Title#2", __rowNum__: 2}
2: {MeetingId: "49490", AgendaItem: "3", LegistarID: "61168", Title: "Titel#3", __rowNum__: 3}
3: {MeetingId: "49490", AgendaItem: "4", LegistarID: "61612", Title: "Title#4", __rowNum__: 4}
4: {MeetingId: "49490", AgendaItem: "5", LegistarID: "60646", Title: "Title#5", __rowNum__: 5}

Would it be possible that someone can show me how to achieve this?有人可以告诉我如何实现这一目标吗?

Thank you, Erasmo谢谢你,埃拉斯莫

If you are literally just wanting to add the ID "49490" to each iteration, then this will do it.如果您实际上只是想在每次迭代中添加 ID“49490”,那么就可以了。

$scope.AgedaItems.forEach(function(item){
    item.MeetingId = "49490";
})

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

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