skip to Main Content

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


  1. A reduce and a filter could do it

    const filteredArray = Object.values(
      response.reduce((acc, curr) => {
        const { lineId, oeeCalculations: { others: { totalTime } } } = curr;
    
        acc[lineId] ??= { ...curr, totalTimeSum: 0 }; // Initialize if not already present
        acc[lineId].totalTimeSum += totalTime;
    
        if (acc[lineId].totalTimeSum > 744) {
          acc[lineId].included = true; // Mark it as valid for inclusion
        }
    
        return acc;
      }, {})
    ).filter(item => item.included)
     .map(({ totalTimeSum, included, ...rest }) => rest); // Remove extra fields
    
    console.log(filteredArray);
    <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>
    Login or Signup to reply.
  2. const duplicateItems = [];
    const alreadyAdded = new Set();
    
    response.forEach((item, index, self) => {
      const totalTime = item.oeeCalculations.others.totalTime;
      if (!alreadyAdded.has(item.lineId)) {
        const duplicate = self.find((nItem, nIndex) =>
          nIndex !== index && nItem.lineId === item.lineId &&
          nItem.oeeCalculations.others.totalTime + totalTime > 744
        );
    
        if (duplicate) {
          duplicateItems.push(item);
          alreadyAdded.add(item.lineId);
        }
      }
    })
    console.log(duplicateItems)
    <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>
    Login or Signup to reply.
  3. You could group the items by lineId and take only the groups/first items of wanted sums.

    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 } } }],
        getTime = ({ oeeCalculations: { others: { totalTime } } }) => totalTime,
        result = Object
            .values(Object.groupBy(response, ({ lineId }) => lineId))
            .flatMap(group => group.length !== 1 && group.reduce((t, o) => t + getTime(o), 0) >= 744
                ? group[0]
                : []
            );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search