I am getting below response from API , i wanted to create new array based on below condition and push result into new array
I wanted to filter out all duplicate objects based on lineId and totalTime
Below Condition to filter
For ex – If lineId is same and Sum of totalTime in both the lineId is more than 744, than need to push this object into new Array.
For ex – In below response , object with lineId 333 is coming twice and their totalTime sum is more than 744,
than i want this object to be pushed into new array.
Can anyone help me to do this.
const response = [
{
"lineId": "333",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "333",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "111",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "111",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "1115",
"oeeCalculations": {
"others": {
"totalTime": 200
}
}
}
];
Expected Output
const result = [
{
"lineId": "333",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "111",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
}
];
I tried below code but its not summing value of totalTime and not working as expected
const foundDuplicateName = response.find((nnn, index) => {
return response.find((x, ind) => x.lineId === nnn.lineId && x.oeeCalculations.others.totalTime >= 744 && nnn.oeeCalculations.others.totalTime >= 744 && index !== ind)
})
console.log(foundDuplicateName)
<script>
const response = [{
"lineId": "333",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "333",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "111",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "111",
"oeeCalculations": {
"others": {
"totalTime": 744
}
}
},
{
"lineId": "1115",
"oeeCalculations": {
"others": {
"totalTime": 200
}
}
}
];
</script>
3
Answers
A reduce and a filter could do it
You could group the items by
lineId
and take only the groups/first items of wanted sums.