1

I have a array of objects, like:

[{
  actionType: 10,
  orderItemId: "3205ae52-ab00-4823-a004-da0cda639065",
  productComponentAction:{
    productComponent:{
      offerId: 10002839,
      parentOfferId: 10003058,
      adoptableProdCompId: undefined
    } 
  }
},
{
  actionType: 10,
  orderItemId: "3205ae52-ab00-4823-2121-da0cda6390ae",
  productComponentAction:{
    productComponent:{
      offerId: 10002839,
      parentOfferId: 10003058,
      adoptableProdCompId: undefined
    } 
  }
}]

I want this array to be unique on the basis of offerId. If offerId is the same, then I want to remove that object from the array.

4 Answers 4

2

You can just use a Map, with reduce, and grab the values from the map:

const arr = [{
    actionType: 10,
    orderItemId: "3205ae52-ab00-4823-a004-da0cda639065",
    productComponentAction: {
      productComponent: {
        offerId: 10002839,
        parentOfferId: 10003058,
        adoptableProdCompId: undefined
      }
    }
  },
  {
    actionType: 10,
    orderItemId: "3205ae52-ab00-4823-2121-da0cda6390ae",
    productComponentAction: {
      productComponent: {
        offerId: 10002839,
        parentOfferId: 10003058,
        adoptableProdCompId: undefined
      }
    }
  }
]

const filtered = Array.from(arr.reduce((a, v) => {
  const id = v.productComponentAction.productComponent.offerId
  if(!a.has(id)) a.set(id, v)
  return a
}, new Map()).values())

console.log(filtered)

Sign up to request clarification or add additional context in comments.

Comments

2

You can use a temporary Map, keyed by whatever you want to be unique. Its constructor accepts [key,value] pairs:

let data = [{actionType: 10, orderItemId: "3205ae52-ab00-4823-a004-da0cda639065", productComponentAction:{productComponent:{ offerId: 10002839, parentOfferId: 10003058,     adoptableProdCompId: undefined } }},{actionType: 10,orderItemId: "3205ae52-ab00-4823-2121-da0cda6390ae", productComponentAction:{ productComponent:{ offerId: 10002839,      parentOfferId: 10003058, adoptableProdCompId: undefined  }  }}];

let uniques = Array.from(new Map(
     data.map(item => [item.productComponentAction.productComponent.offerId, item])
).values());
    
console.log(uniques);

Comments

0

You can create an object which would have objectId as its key, like this:

var target = {};
for (let item of myArray) {
    if (target[item.objectId] === undefined) {
        target[item.objectId] = item;
    }
}

and then, when you attempt to add to this array, you just check whether the item with the certain key already exists.

Comments

0

It is possible to use reduce method. It checks whether there is a property with the same key and if it exists it creates a key, otherwise it does nothing:

arr.reduce((a, c)=> {
    a[c.productComponentAction.productComponent.offerId] = 
        a[c.productComponentAction.productComponent.offerId] || {...c};
    return a;
}, {})

An example:

let arr = [{
  actionType: 10,
  orderItemId: "3205ae52-ab00-4823-a004-da0cda639065",
  productComponentAction:{
    productComponent:{
      offerId: 10002839,
      parentOfferId: 10003058,
      adoptableProdCompId: undefined
    }
  }
},
{
  actionType: 10,
  orderItemId: "3205ae52-ab00-4823-2121-da0cda6390ae",
  productComponentAction:{
    productComponent:{
      offerId: 10002839,
      parentOfferId: 10003058,
      adoptableProdCompId: undefined
    }
  }
}];

let filteredList = arr.reduce((a, c)=> {
  a[c.productComponentAction.productComponent.offerId] = 
      a[c.productComponentAction.productComponent.offerId] || {...c};
  return a;
}, {})

console.log(Object.values(filteredList));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.