Filter over an Array

Hey, I’m trying to use the Filter over an Array, but although when debugging it appears to be filtering, the end result is the same original array. Ex below:

let orig_arr = ["A", "A#/Bb", "B", "C", "C#/Db", "D", "D#/Eb", "E", "F", "F#/Gb", "G", "G#/Ab"];
let new_arr = this.orig_arr.filter((elm) => {
if(elm.indexOf("/")!=-1) {
elm = elm.split("/")[0];
return tun;

What I want is the new_arr to be:

["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"];

But it ends like a clone of the orig_arr, eg same elms.

Thanks in advance.

  1. You don’t need this
  2. Use proper indenting
  3. tun is not defined? Maybe you wanted to return the direct result of elm.indexOf("/") So there is not even a need to have if. just return elm.indexOf("/") != -1
  4. Or if you want cleaner code: orig_arr.filter(el => !el.includes('/'))

By looking closer at your question, I see that filter is not what you want :slight_smile:
In this case you need a .map. You only use filter if you actually want to remove from an array.

Here is the solution: => note.includes('/') ? note.split('/')[0] : note)

Thanks MartinMuzatko for the tip:

Sorry : elm instead of tun

The this is because the code is inside a class, more precisely in a constructor.

I’ll learn about the map method and I’ll try it.


Update: map worked perfectly! Thanks for the lesson!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.