0

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. It gets populated by excel data.

It contains:

current array

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.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

erasmo carlos
  • 664
  • 5
  • 16
  • 37
  • You just loop trough the array and add/edit property – Akxe Aug 27 '20 at 21:24
  • 1
    Does this answer your question? [Add property to an array of objects](https://stackoverflow.com/questions/38922998/add-property-to-an-array-of-objects) – Heretic Monkey Aug 27 '20 at 21:26
  • FYI, [`push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) is the name of the method to add an element to the array. Since you're not adding the property to the array, but rather adding the property to each object in the array, push is not the correct term, and may lead people astray. – Heretic Monkey Aug 27 '20 at 21:28
  • @HereticMonkey - thank you, I have updated the question's title. – erasmo carlos Aug 27 '20 at 21:30

1 Answers1

1

If you are literally just wanting to add the ID "49490" to each iteration, then this will do it.

$scope.AgedaItems.forEach(function(item){
    item.MeetingId = "49490";
})
billy_comic
  • 867
  • 6
  • 27