Reputation: 1274
So i have a little encounter I can't seem to know whats the problem because i can't seem to get the vm.dataUpdate.length
its returning me an undefined value.
function massUpdate() {
vm.dataUpdate = Object.assign({}, vm.leaveList);
console.log(vm.dataUpdate.length); // returning undefined
for (var x = 0; x < vm.dataUpdate.length; x++) {
console.log(x);
if (vm.dataUpdate[x].actionStatus === 'edited') {
vm.dataUpdate[x].leaveStatus = vm.dataUpdate[x].action.actionName;
console.log(vm.dataUpdate = vm.dataUpdate[x]);
}
}
}
EDIT
vm.leaveList value
[
{
"_id": "5a0e86e1cd39a911e3be8252",
"comment": "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"",
"leaveSupervisor": "Barrack Obama",
"leaveDays": "2",
"leaveType": "Sick Leave",
"fullName": "michelle obama",
"toDate": "2017-11-17T16:00:00.000Z",
"fromDate": "2017-11-16T16:00:00.000Z",
"user": "admin123",
"__v": 0,
"leaveDateCreated": "2017-11-17T06:51:13.570Z",
"leaveStatus": "Pending Approval"
},
{
"_id": "5a0ea449cd39a911e3be8253",
"comment": "my birthday",
"leaveSupervisor": "Barrack Obama",
"leaveDays": "1",
"leaveType": "Birthday Leave",
"fullName": "michelle obama",
"toDate": "2017-11-20T16:00:00.000Z",
"fromDate": "2017-11-20T16:00:00.000Z",
"user": "admin123",
"__v": 0,
"leaveDateCreated": "2017-11-17T08:56:41.060Z",
"leaveStatus": "Cancelled"
}]
Any Suggestions are welcomed. Thanks
Upvotes: 0
Views: 41
Reputation: 1091
You should pass an array to Object.assign. See the following code:
var arr = [1,2,3]
var a = Object.assign({}, arr)
var b = Object.assign([], arr)
console.log(a) // { '0': 1, '1': 2, '2': 3 }
console.log(b) // [ 1 , 2 , 3 ]
EDIT
Because you are using angular, you can try also angular.copy it makes a deep copy of your object (array in this case) so you can avoid side effects when changing the properties of vm.dataUpdate
vm.dataUpdate = angular.copy(vm.leaveList)
Upvotes: 2