【问题标题】:AngularJS - How to add new property to each object in arrayAngularJS - 如何为数组中的每个对象添加新属性
【发布时间】:2020-12-16 19:54:51
【问题描述】:

我想学习如何将范围变量推送到数组的每个项目中。

这就是我所拥有的:

$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;
        });
};

数组是 $scope.AgendaItems。它由 excel 数据填充。

它包含:

我必须将名为 $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}

有人可以告诉我如何实现这一点吗?

谢谢你, 伊拉斯莫

【问题讨论】:

  • 您只需遍历数组并添加/编辑属性
  • 这能回答你的问题吗? Add property to an array of objects
  • 仅供参考,push 是向数组添加元素的方法的名称。由于您不是将属性添加到数组,而是将属性添加到数组中的每个对象,因此 push 不是正确的术语,可能会让人误入歧途。
  • @HereticMonkey - 谢谢,我已经更新了问题的标题。

标签: javascript arrays angularjs


【解决方案1】:

如果您只是想在每次迭代中添加 ID“49490”,那么就可以了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-12
    • 2016-11-27
    • 2015-03-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多