1

I am trying to use array.splice to remove an object from my array, but my object position in the array is -1 so its not being removed. I think its because I am creating a new object every time, so the two object are not the same. Here is my code.

let selectedCurrency = new Currency(null, null, null, symbol, currency_code, false, false, currency_description, null);

    if (event.checked) {

      this.companyCurrencies.push(selectedCurrency);

    } else {
      console.log("original array: ",this.companyCurrencies);

      console.log("selected currency: ",selectedCurrency);

      let index = this.companyCurrencies.indexOf(selectedCurrency);

      if (index === -1) {
        return;
      }

      this.companyCurrencies.splice(index, 1);
      console.log("array after spliced: ",this.companyCurrencies);
    }

I am not sure how to get around this issue, any help would be much appreciated thanks.

2
  • So look for a unique identifier Commented Apr 27, 2017 at 14:59
  • Thanks dude, managed to get it working with this.companyCurrencies.splice(this.companyCurrencies.findIndex(item => item.code === currency_code), 1); Commented Apr 27, 2017 at 15:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.