This is probably a pretty known issue, and probably already solved in Stack Overflow, but I've searched without any luck.
I have this array which consists of a JSON-string. I'm trying to loop through the array, and convert the JSON to an observablearray so I can access the properties "value", "name", "price" so I can use it with Knockout.
Array with JSON :
0
:
"{"value":"382","name":"Entrecoté med poteter og sånt..","price":295.0}"
1
:
"{"value":"385","name":"Svinekoteletter","price":295.0}"
2
:
"{"value":"386","name":"Pizza Margherita","price":255.0}"
Initial loop :
completeArray.forEach(function(c) {
// convert the json string to an observableArray ??
});
Edited
If someone else comes to find a solution to how to do this, I include the complete example below:
self.addFoodItemsToSubMenu = function (item) {
var existingFIS = JSON.parse(ko.toJSON(item.foodItemList()));
var newFIS = JSON.parse(ko.toJSON(self.selectedFoodItems()));
var completeFIS = existingFIS.concat(newFIS);
var resultArray = ko.observableArray(completeFIS.map(function (item) {
var parsedResult = JSON.parse(item);
var resultObject = {
value: parsedResult.value,
name: parsedResult.name,
price: ko.observable(parsedResult.price)
}
return resultObject;
}));
item.foodItemList(resultArray());
self.selectedFoodItems([]);
}