Although this may be a common issue with many solutions available on Stack Overflow, my search so far has been unsuccessful.
I have an array containing JSON strings. My goal is to loop through the array, convert the JSON into an observable array, and then access the properties "value", "name", and "price" in order to use them 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) {
// How do I convert the JSON string to an observableArray ??
});
Edited
If anyone else is looking for a solution, I've included 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([]);
}