Convert this:
{\"items\":[{\"id\":\"BLE89-A0-123-384\",\"weight\":\"100\",\"quantity\":3},
...
{\"id\":\"BLE10-A0-123-321\",\"weight\":\"
You can try with:
var obj = {
"items":[
{"id":"BLE89-A0-123-384","weight":"100","quantity":3},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4}
],
"country":"JUS",
"region":"A",
"timeout":"FILLER"
};
var quantities = {};
obj.items.forEach(function (item) {
quantities[item.id] = item.quantity;
});
quantities
will then be the object {"BLE89-A0-123-384":3,"BLE10-A0-123-321":4}
. forEach is a native method of array objects in JavaScript that lets you iterate through their elements. You may want to put that piece of code inside a function:
function getQuantities(obj) {
var quantities = {};
obj.items.forEach(function (item) {
quantities[item.id] = item.quantity;
});
return quantities;
}