Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
How to remove underscore from loop items?

I am trying to remove all the _er and _bx from the array, how can I do it? The way I tried doesn't seem to work. I'd like to see a solution where it removes all after _, and aswell only the letter that I put in for e.g remove all _ with er after.

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};

const newArr = []
for (let [key, item] of nullValue.collection.entries()) {
  item.name.replace(/_er/g, '')
  newArr.push(item)
}

console.log(newArr)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Is this what you're looking for?

const nullValue = {
  collection: [
    {
      name: 'test_er',
    },
    {
      name: 'test_bx',
    },
    {
      name: 'fred',
    },
    {
      name: 'test_er',
    },
  ],
};

nullValue.collection = [
  ...nullValue.collection.map(item => ({
    name: item.name.replace(/_.*$/, ''),
  })),
];

console.log(nullValue);

about 4 years ago · Juan Pablo Isaza Report

0

You can also use .split('_')[0] with the map method similar to Dmitry's answer... This gives you the first string of the split array, split at the underscore...

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};

nullValue.collection = [ ...nullValue.collection.map( names => ({ name: names.name.split('_')[0], })),]
console.log(nullValue)

If you want to keep the original array of objects...

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};
const newArr = { collection : 
[ ...nullValue.collection.map( names => 
  ({ name: names.name.split('_')[0], })),
]}

console.log('newArr = ', newArr)
console.log('nullValue = ', nullValue)

about 4 years ago · Juan Pablo Isaza Report

0

const nullValue = {
    collection: [
     {
     name: "test_er"
},
    {
     name: "test_bx"
},
    {
     name: "fred"
},
    {
     name: "test_er"
}
    ]
};

nullValue.collection = nullValue.collection.map(i => i.name.replace(/_.*$/, ''))
console.log(nullValue)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!