How to filter duplicate object from array of objects based on few conditions?

Discussão em 'Angular' iniciado por Tinku Tiwari, Setembro 24, 2024.

  1. Tinku Tiwari

    Tinku Tiwari Guest

    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": "222",
    "oeeCalculations": {
    "others": {
    "totalTime": 500
    }
    }
    },
    {
    "lineId": "111",
    "oeeCalculations": {
    "others": {
    "totalTime": 500
    }
    }
    },
    {
    "lineId": "111",
    "oeeCalculations": {
    "others": {
    "totalTime": 200
    }
    }
    }
    ];


    Expected Output

    const result = [
    {
    "lineId": "333",
    "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": "222",
    "oeeCalculations": {
    "others": {
    "totalTime": 500
    }
    }
    },
    {
    "lineId": "111",
    "oeeCalculations": {
    "others": {
    "totalTime": 500
    }
    }
    },
    {
    "lineId": "111",
    "oeeCalculations": {
    "others": {
    "totalTime": 200
    }
    }
    }
    ];
    </script>

    Continue reading...

Compartilhe esta Página