Within my angular application, I am faced with the challenge of working with two arrays structured as follows:
$scope.data = ["val 10,val 11,val 12", "val 20,val 21,val 22", "val 30,val 31,val 32"];
$scope.fields = [ "key1", "key2", "key3" ];
My task is to properly format and send each element of the $scope.data
array to the server in the following manner:
{
key1: 'val 10',
key2: 'val 11',
key2: 'val 12',
}
{
key1: 'val 20',
key2: 'val 21',
key2: 'val 22',
}
{
key1: 'val 30',
key2: 'val 31',
key2: 'val 32',
}
However, a significant issue arises during this process; upon sending the data to the server, only the last value of $scope.data
is being received. Reviewing my code below:
// $scope.data = ["val 10,val 11,val 12", "val 20,val 21,val 22", "val 30,val 31,val 32"];
_.each($scope.data, function(result, index) {
var dataArray = result.split(",");
_.each(dataArray, function(info, position) {
if( info !== null || info !== '' || info.length > 0 ) {
$scope.data[ $scope.fields[position] ] = info;
}
});
$http
.post(url + '/import-data', {
"data": $scope.data
})
.then(function (res) {
console.log(res);
}, function (error) {
console.log("error : ", error);
})
});
I am seeking advice on resolving this dilemma. Appreciate any insight you can offer. Thank you.