const sum = info.data.map(x => x.achat) // Extract only the achat field
.flat() // Flatten the array Eg: [[1], [2], [3]] --> [1, 2, 3]
.map(x => +x.initial) // Extract the initial field ("+" is to convert string to integer)
.reduce((e, f) => e + f); // Calculate the sum
let info = {
"data": [
{
"id": 1,
"achat": [
{
"fournisseur": "aze",
"initial": "12",
"supp": "0"
}
]
},
{
"id": 2,
"achat": [
{
"fournisseur": "paul",
"initial": "123",
"supp": "100"
}
]
},
{
"id": 3,
"achat": [
{
"fournisseur": "Kian Crist",
"initial": "12",
"supp": "0"
}
]
},
{
"id": 4,
"achat": [
{
"fournisseur": "Kian Crist",
"initial": "0",
"supp": "0"
}
]
},
{
"id": 5,
"achat": [
{
"fournisseur": "LOUBATA",
"initial": "12345098",
"supp": "123400"
}
]
},
{
"id": 6,
"achat": [
{
"fournisseur": "Douze Pixels",
"initial": "10000000",
"supp": "300000"
}
]
},
{
"id": 7,
"achat": [
{
"fournisseur": "azer",
"initial": "1234",
"supp": "0"
}
]
}
]
};
const sum = info.data.map(x => x.achat) // Extract only the achat object
.flat() // Flatten the array Eg: [[1], [2], [3]] --> [1, 2, 3]
.map(x => +x.initial) // Extract the initial field ("+" is to convert string to integer)
.reduce((e, f) => e + f); // Calculate the sum
console.log("sum: " + sum);