0

I have something like this:

  items: { _id: number; place: SomeAddressClassDto }[] = [];
  sessionItem: { _id: number; place: SomeAddressClassDto };

  createAddressList() {

    this.service.getWorkingPlaces().subscribe((items) => {
      this.items = items;

      this.sessionItem = {
        place: JSON.parse(sessionStorage.getItem('currentPlace')),
        _id: this.items.length,
      };

      this.items.push(this.sessionItem);

    // _.uniqBy(this.items, 'place')

      const idx = items.findIndex((item) => this.service.comparePlaces(item.place, 
 this.service.getCurrentWorkingPlace()));
      if (idx !== -1) this.radiobox.option = `${idx}`;
    });
  }

I am trying to remove any duplicates from 'items' array using the method _uniqBy, but it isn't working. I think it's because for this items._id are always different but items.place could be equal and if are I would like to get rid of this.

Maybe better way is to check is the same items.place is already in that array, but no idea how to do this.

EDIT: To be more specify, items looks like this:

0: place: {name, street, city, etc...}
   _id: 0

So it is possible to have very similar object whitch is differnet only by one property in place{}

3 Answers 3

0

I would suggest using the _id to determine if the value is unique. If you use place the value may always be unique since they could be different instances of the same class.(I can't tell from your code if this is the case)

createAddressList() {
    this.service.getWorkingPlaces().subscribe((items) => {
        this.items = items;

        this.sessionItem = {
            place: JSON.parse(sessionStorage.getItem('currentPlace')),
            _id: this.items.length,
        };

        this.items.push(this.sessionItem);

        _.uniqBy(this.items, '_id');

        const idx = items.findIndex((item) => this.service.comparePlaces(item.place, this.service.getCurrentWorkingPlace()));
        if (idx !== -1) {
            this.radiobox.option = `${idx}`;
        }
    });
}

Alternately, you could use a property from inside place to determine if they are unique like this:

 _.uniqBy(this.items, (x) => x.place && x.place.someOtherPropertyInsideOfPlace);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array.Prototype.reduce() and Array.prototype.find() to create an array that has no duplicates based on the place property.

As an example:

const test = [
  { _id: 1, place: 'a' },
  { _id: 2, place: 'a' },
  { _id: 3, place: 'a' },
  { _id: 4, place: 'b' },
  { _id: 5, place: 'c' },
]

const noDups = test.reduce((accum, current)=> {
  if(!accum.find(item => item.place === current.place))
    accum.push(current)
  return accum
}, [])
console.log(noDups)

reduce() will create a new array (in this case, the accum variable is being initialized as an empty array), but only add new items in the new array if the .place property is unique, if it doesn't already find an item with the same .place property

Comments

0
  • declaring a simple object array with id and place properties.

  • Removing duplicates by javascript filter function.

  • filter function will remove the objects from array whose place is paris.

var arr = [{
    _id: 1,
    place: 'paris'
  },
  {
    _id: 2,
    place: 'tokyo'
  },
  {
    _id: 3,
    place: 'ontario'
  },
  {
    _id: 4,
    place: 'paris'
  },
  {
    _id: 5,
    place: 'karachi'
  },
  {
    _id: 6,
    place: 'paris'
  },
  {
    _id: 7,
    place: 'new york'
  }
];

var newArray = arr.filter((self, item, index) => index.findIndex(t => (t.place === self.place)) === item);

console.log(newArray);

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.