I want to add a variable from array of objects and combine them.
Example data :
const item = [{
"act": "Q",
"line": 1,
"qty": 6,
"type": 00
},
{
"act": "Q",
"line": 1,
"qty": 6,
"type": 00
},
{
"act": "Q",
"line": 2,
"qty": 6,
"type": 00
},
{
"act": "Q",
"line": 2,
"qty": 4,
"type": 00
}]
I want to check if act is Q and type is 00 then get the output with line and sum of qty for the line.
[{
"line": 1,
"qty": 12,
},
{
"line": 2,
"qty": 10,
}]
2
Answers
So basically what you would want to do is use the
Array.filter
function and set in the parameters to get what you want.Something like this
the variable
a
would now contain all your objects, you can iterate it and print whatever you wantfor your input:
To filter by act and type you can use
Array.filter()
method like this:then to sum the qty you can use
Array.reduce()
method like this:output will be exact what you need: