Reputation: 124
I have an issue. I am trying to pull data from a table to display edit and submit on my node app. It's a MySQL database and I have no control over it or its design. It only has 2 columns
PropertyName | value
Now when I use knexJS I get my json object
[{“propertyName”: “jobNumbers”, “value”: “20”},{
and so on. I am trying to sort it server side that it does
[{“jobNumbers”: “20”},{
As this will be simpler to display and manipulate should more rows be added or removed later on as I am currently displaying the data like so
$scope.data = response.data;
$scope.nJobNumbers = parseInt($scope.data[0].value)
Which isn’t the best
And help would be great
Upvotes: 1
Views: 252
Reputation: 28722
I'm not sure this is what you are looking for.
This makes a new object out of all given key values pairs, where the propertyNames will be an item on the object with associated value.
function dataParser($data) {
var result = {};
for(var c = 0; c < $data.length;c++) {
var item = $data[c];
result[item.propertyName] = item.value;
}
return result;
}
console.log(dataParser([
{"propertyName": "jobNumbers", "value": "20"},
{"propertyName": "jobTime", "value": "2000"},
{"propertyName": "jobParticipants", "value": "3"},
]));
Upvotes: 0
Reputation: 13943
Using Array#map()
const arr = [{"propertyName": "jobNumbers", "value": "20"}];
const finalArr = arr.map(a=>{
let x = {};
x[a.propertyName] = a.value;
return x;
});
console.log(finalArr)
Upvotes: 1