I made it in the case you are using just the object, without set up as an array.
let info = {
0: { ProId: "Space", Name: "cake", Quantity: "1" },
1: { ProId: "new", Name: "walk", Quantity: "2" },
2: { ProId: "foo", Name: "bar", Quantity: "3" }
}
function incrementQuantify(obj) {
let _newInfo = {};
for (const key of Object.keys(obj)) {
info[key].Quantity = parseInt(info[key].Quantity, 10) + 1;
_newInfo[key] = info[key];
}
return _newInfo;
}
/*
{
'0': { ProId: 'Space', Name: 'cake', Quantity: 2 },
'1': { ProId: 'new', Name: 'walk', Quantity: 3 },
'2': { ProId: 'foo', Name: 'bar', Quantity: 4 }
}
*/
console.log(incrementQuantify(info));
Shorter way:
let info = { 0: { ProId: "Space", Name: "cake", Quantity: "1" }, 1: { ProId: "new", Name: "walk", Quantity: "2" }, 2: { ProId: "foo", Name: "bar", Quantity: "3" } }
for (const key of Object.keys(info)) info[key].Quantity = parseInt(info[key].Quantity, 10) + 1;
console.log(info);
edit: I already made an way to work with arrays too!
let info = [
{ ProId: "Space", Name: "cake", Quantity: "1" },
{ ProId: "new", Name: "walk", Quantity: "2" },
{ ProId: "foo", Name: "bar", Quantity: "3" }
]
function incrementQuantify(obj) {
let newObj = obj;
newObj.Quantity = parseInt(obj.Quantity, 10) + 1;
return newObj;
}
info.map(incrementQuantify);
/*
[
{ ProId: 'Space', Name: 'cake', Quantity: 2 },
{ ProId: 'new', Name: 'walk', Quantity: 3 },
{ ProId: 'foo', Name: 'bar', Quantity: 4 }
]
*/
console.log(info);
Shorter way:
let info = [ { ProId: "Space", Name: "cake", Quantity: "1" }, { ProId: "new", Name: "walk", Quantity: "2" }, { ProId: "foo", Name: "bar", Quantity: "3" } ]; info.map(o => o.Quantity = parseInt(o.Quantity, 10) + 1);
console.log(info);
Hope it helps!
console.log(Array.isArray(info))?infois indeed an Array. You can have a look at the solutions below suggesting to loop through it with Array.forEach or Array.map