I am working on an app that read an excel file and upload its contents to a ui.grid. That part is working good. I need to also create a second array with the same data but a different purpose, to use on a 3rd party api call.
The array needs to have this format:
{
"users": [
{
"name": "frank",
"email": "[email protected]"
},
{
"name": "mike",
"email": "[email protected]"
}
]
}
currently my array looks like:
data: Array(5)
0:
Email: "[email protected]"
Name: "Jack Anderson"
1:
Email: "[email protected]"
Name: "Ed Johnson"
2:
Email: "[email protected]"
Name: "Dead Poole"
3:
Email: "[email protected]"
Name: "Hank Schmidt"
4:
Email: "[email protected]"
Name: "Steven Alves"
The code I am using:
$scope.arr = new Array;
// SEND THE FILES.
$http(request)
.then(function (data) {
angular.forEach(data.data, function (item) {
var b = {
Name: item.Name,
Email: item.Email
};
$scope.arr.push(b);
});
}).error(function (error) { });
}
$scope.gridData = { data: 'arr' };
How can I change the code, or add code, to create an array that meets the format I need?
Thank you for your help. Erasmo
UPDATE
This is my code after adding the recommendation
.controller('myController', function ($http) {
var formdata = new FormData();
var vm = this;
vm.getTheFiles = function ($files) {
angular.forEach($files, function (value, key) {
formdata.append(key, value);
});
};
// SEND FILES TO THE API USING POST METHOD.
vm.uploadFiles = function () {
var request = {
method: 'POST',
url: '/api/fileupload/',
data: formdata,
headers: {
'Content-Type': undefined
}, transformRequest: angular.identity
};
vm.arr = new Array;
// SEND THE FILES.
$http(request).then(function (data) {
angular.forEach(data.data, function (item) {
var b = {
name: item.Name,
email: item.Email
};
vm.arr.push(b);
});
}).catch(function (error) {
console.log("Error: " + error);
});
var items = vm.arr.map((item) => {
return {
name: item.name,
email: item.email
};
});
var panelists = {
"panelists": items
}
console.log(panelists);
}
vm.gridData = { data: 'arr' };
ES6?