#how to remove element from array of string

8 messages · Page 1 of 1 (latest)

weak fog
#

My code:

```readonly frequencyOptions: SelectItem<string>[];```

Select Item is defined like:

    label: string | Observable<string>;
    value: T | Observable<T>;
}```

In costructor:

       ``` this.frequencyOptions = this.sharedDataService.frequencyOptions;```

this.sharedDataService.frequencyOptions is equal to:

```    readonly frequencyOptions: SelectItem<string>[] = [
        { value: 'once', label: this.translate.stream(APP_TRANSLATIONS.common.frequency.interval.once) },
        { value: 'perMonth', label: this.translate.stream(APP_TRANSLATIONS.common.frequency.interval.perMonth) },
        { value: 'custom', label: this.translate.stream(APP_TRANSLATIONS.common.frequency.interval.custom) },
    ];```

and then this.frequencyOptions has those 3 elements in the array, how i can manually remove custom element from the this.frequencyOptions?
low holly
#

use filter method

#

[1, 2, 3, 4].filter(n => n % 2 == 0) filtered out odd numbers

weak fog
#

i want t remove element by value

bleak anvil
#

please adapt the code, is not difficult

#

[1, 2, 3, 4].filter(n => ..... here condition..... )

lost hinge
#

The above is incomplete. filter() doesn't remove anything from an array. It creates a new array with only the elements matching the predicate. So you would to reassign that new array to your variable. But your variable is readonly, so the compiler won't let you do that.
So either that's what you want (a copy), and you need to do that and remove the readonly modifier, or you want to actually remove an element from the array, and then you need to use the splice method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
You should read that whole Array documentation, because it contains stuff that you'll need very often.

weak fog
#

thanks people!!