0

I have a requirement where I have data similar to this:

[
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-13 18:04:34.356251+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "E10003",
      "active": "A",
      "classCode": "EX002",
      "publishedTime": "2022-02-21 03:34:10.139843+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-23 12:43:00.08635+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "T",
      "classCode": "EX004",
      "publishedTime": "2022-02-24 12:00:00.08635+00"
    }
  }
]

I need to eliminate the duplicates for each positionCode. For example if there are multiple objects with the same positionCode, I need to retain only the latest one in the array and remove all the other duplicates from the array and get back the array in the same format with the duplicates removed.

Please suggest a way to do this in Javascript.

I thought of grouping the objects using the positionCode and then checking which is the latest. Not sure how to proceed and would appreciate help on this.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Feb 28 '22 at 07:23

1 Answers1

0

You can achieve this by filtering positionCodes based on single item and compare their publishedTime if current item publishedTime is greather than the item which is already in our array (arr) or item which is unique to them. Then it will push to temporary array otherwise not.

let arr =[
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-13 18:04:34.356251+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "E10003",
      "active": "A",
      "classCode": "EX002",
      "publishedTime": "2022-02-21 03:34:10.139843+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-23 12:43:00.08635+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "T",
      "classCode": "EX004",
      "publishedTime": "2022-02-24 12:00:00.08635+00"
    }
  }
]

let a=[]
arr.forEach((item,index)=>{
   let i=arr.filter(e=>e.payload.positionCode===item.payload.positionCode && new Date(e.payload.publishedTime)>new Date(item.payload.publishedTime));
   if(!i.length) a.push(item);
});
console.log(a) // here is your final result array
Asad Haroon
  • 476
  • 3
  • 9