skip to Main Content

I have below array with objects which includes duplicates type values. I need unique array of objects based on immediate same type values.

Note:
If any object has immediate next same type values then it will make one unique array list. Else it will include another unique object list with same type value.

Array :

[ 
  { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, 
  { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, 
  { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } 
]

In above array object 2 is immediate after object 1 with same type A value therefore it make one unique list with A. And after that objects 3,4,5 has immediate basis on type B value therefore it created another unique list. After object 2, object 3 there is no similar "type" value so first unique list end there. But again object 5,6,7,8 has immediate basis type A value therefore it created another unique list.

I expected below Output (if any object has one after another same value then it will create unique list else it will end there and make another separate objects list) :

[
   {
      "A":[
         {
            "object":1,
            "start":"2018-07-08",
            "end":"2018-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":2,
            "start":"2018-12-31",
            "end":"2019-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   },
   {
      "B":[
         {
            "object":3,
            "start":"2019-06-26",
            "end":"2019-12-31",
            "type":"B",
            "user":"PQRS"
         },
         {
            "object":4,
            "start":"2019-12-31",
            "end":"2020-06-31",
            "type":"B",
            "user":"PQRS"
         }
      ]
   },
   {
      "A":[
         {
            "object":5,
            "start":"2021-12-31",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":6,
            "start":"2012-12-31",
            "end":"2022-06-26",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":7,
            "start":"2022-06-26",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":8,
            "start":"2022-12-31",
            "end":"2023-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   }
]

3

Answers


  1. What you want is something like lodashs uniqBy, see documentation here https://lodash.com/docs/4.17.15#uniqBy

    var input = [ { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } ]
    
    var output = _.uniqBy(input, 'type')
    
    // OR
    
    var output = _.uniqBy(input, x => x.type)
    
    // results in 
    [
       {
          "object": 1,
          "start": "2018-07-08",
          "end": "2018-12-31",
          "type": "A",
          "user": "ABCD"
       },
       {
          "object": 3,
          "start": "2019-06-26",
          "end": "2019-12-31",
          "type": "B",
          "user": "PQRS"
       }
    ]
    
    
    Login or Signup to reply.
  2. Try if it works for you.

    function createUniqueLists(inputArray) {
      const result = [];
      let currentList = [];
    
      for (let i = 0; i < inputArray.length; i++) {
        const currentObject = inputArray[i];
        const currentType = currentObject.type;
    
        if (i === 0 || currentType !== inputArray[i - 1].type) {
          // Start a new unique list for a different type
          if (currentList.length > 0) {
            result.push({ [currentList[0].type]: currentList });
            currentList = [];
          }
        }
    
        currentList.push(currentObject);
    
        // If the next object has a different type, create a new unique list
        if (i < inputArray.length - 1 && currentType !== inputArray[i + 1].type) {
          result.push({ [currentType]: currentList });
          currentList = [];
        }
      }
    
      return result;
    }
    
    const inputArray = [
      { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" },
      { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" },
      { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" },
      { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" },
      { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" },
      { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" },
      { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" },
      { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" }
    ];
    
    const output = createUniqueLists(inputArray);
    console.log(JSON.stringify(output, null, 2));
    Login or Signup to reply.
  3. You could take a closure over type anf check the type from each element. If same, add to the last element of the result set, if not, create a new object. Update type.

    const
        data = [{ object: 1, start: "2018-07-08", end: "2018-12-31", type: "A", user: "ABCD" }, { object: 2, start: "2018-12-31", end: "2019-06-26", type: "A", user: "ABCD" }, { object: 3, start: "2019-06-26", end: "2019-12-31", type: "B", user: "PQRS" }, { object: 4, start: "2019-12-31", end: "2020-06-31", type: "B", user: "PQRS" }, { object: 5, start: "2021-12-31", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 6, start: "2012-12-31", end: "2022-06-26", type: "A", user: "ABCD" }, { object: 7, start: "2022-06-26", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 8, start: "2022-12-31", end: "2023-06-26", type: "A", user: "ABCD" }],
        result = data.reduce((type => (r, o) => {
            if (type === o.type) {
                r.at(-1)[type].push(o);
            } else {
                type = o.type;
                r.push({ [type]: [o] });
            }
            return r;
        })(), []);
    
    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