1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| const JSon={
"stock" : [
{
"Itemid":12,
"location": "Lyon",
"quantity": 12
},
{
"Itemid":12,
"location": "Lyon",
"quantity": 14
},
{
"Itemid":12,
"location": "Brest",
"quantity": 12
},
{
"Itemid":14,
"location": "Brest",
"quantity": 12
},
{
"Itemid":12,
"location": "Brest",
"quantity": 12
}
]
}
console.log(JSON.stringify(JSon.stock))
const t=[],t2=[];
JSon.stock.map(v=>{
if(!t[v.Itemid+" "+v.location]){
t[v.Itemid+" "+v.location]=v.quantity
}
else t[v.Itemid+" "+v.location]+=v.quantity
});
for(i in t){
t2.push({"Itemid":i.split(" ")[0],"location":i.split(" ")[1],"quantity":t[i]})
}
console.log(JSON.stringify(t2)); |
Partager