skip to Main Content

i have below array of object from API response.i want to push only those object whose id is not repeating and totalTime is 0

for ex – in below JSON, object with id = 4 is repeating twice so i dont want to include
this object into new array, here id = 1 and id = 3 have totaltime is 0, than i want to push this one .

Below is my JSON

const response = [
  {  
      "id": "1",
      "oeeCalculations": {
          "others": {
              "totalTime": 0
          }
      }
  },
  {
      "id": "2",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  },
  {
      "id": "3",
      "oeeCalculations": {
          "others": {
              "totalTime": 0
          }
      }
  },
  {
      "id": "4",
      "oeeCalculations": {
          "others": {
              "totalTime": 0
          }
      }
  },
  {
      "id": "4",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  }
];

Expected output –

const newResponse = [
    {  
        "id": "1",
        "oeeCalculations": {
            "others": {
                "totalTime": 0
            }
        }
    },
    {
        "id": "3",
        "oeeCalculations": {
            "others": {
                "totalTime": 0
            }
        }
    }
];

I tried below code, but its returning unique lines only..

 const values = Object.values(
      response.reduce((a, b) => {
        if (!a[b.id]) a[b.id] = b 
          return a
       }, {})
    )

2

Answers


    1. You need to group the entries by id.

    2. Filter the responses array by matching the id, the length of the entries array from each (id) group is 1 and all the entries must fulfill with the oeeCalculations.others.totalTime is equal to 0.

    const groupByIds = response.reduce((acc, cur) => {
        let group = acc.find(x => x["id"] == cur["id"]);
    
        if (group == null) acc.push({ id: cur["id"], entries: [cur] });
        else group["entries"].push(cur);
    
        return acc;
    }, [] as any[]);
    
    const values = response.filter((x) => {
      return groupByIds.find(y => y["id"] === x["id"] 
        && y["entries"].length === 1 
        && y["entries"].every((z: any) => z["oeeCalculations"]["others"]["totalTime"] === 0)
        );
    });
    

    Demo @ TypeScript Playground

    Login or Signup to reply.
  1. Here is another way to achieve the desired result:

    1. Generate an array of all IDs using the map() function
    2. Remove all items which have duplicates IDs using the filter() function, with the help of the previously created ids list
    3. Remove all items which have a totalTime other than 0 using the filter() function
    const ids = response.map(item => item.id);
    const result = response
      .filter(item => ids.filter(id => id === item.id).length === 1)
      .filter(item => item.oeeCalculations.others.totalTime === 0);
    

    Demo:

    const response = [
      {  
          "id": "1",
          "oeeCalculations": {
              "others": {
                  "totalTime": 0
              }
          }
      },
      {
          "id": "2",
          "oeeCalculations": {
              "others": {
                  "totalTime": 744
              }
          }
      },
      {
          "id": "3",
          "oeeCalculations": {
              "others": {
                  "totalTime": 0
              }
          }
      },
      {
          "id": "4",
          "oeeCalculations": {
              "others": {
                  "totalTime": 0
              }
          }
      },
      {
          "id": "4",
          "oeeCalculations": {
              "others": {
                  "totalTime": 744
              }
          }
      }
    ];
    const ids = response.map(item => item.id);
    const result = response
      .filter(item => ids.filter(id => id === item.id).length === 1)
      .filter(item => item.oeeCalculations.others.totalTime === 0);
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search