0

How can I remove description:("Auto-generated by YouTube.") from title?

[
  {
    title: 'DÁKITI description:("Auto-generated by YouTube.")',
    author: 'Bad Bunny',
    duration: 205090
  }
]

Thanks in advance!

4
  • Hope this helps.. codepen.io/Maniraj_Murugan/pen/VwmWgLM Commented Feb 19, 2021 at 6:47
  • @adiga I think this question involes more than just remove text from a string It is also about iterating an array and writing an object property Commented Feb 19, 2021 at 7:18
  • @Aalexander there are hundreds of duplicates on how to iterate over arrays and manipulate a property. And, there are plenty of questions about iterating and replacing particular property like: stackoverflow.com/questions/51667175/… Commented Feb 19, 2021 at 7:46
  • @adiga yes but your original duplicate wasn't appropriate for this question. How to remove text from a string? (12 answers) Commented Feb 19, 2021 at 8:08

2 Answers 2

2

use map array method, i guess you have other elements in the array with the same substring, then for each one use replace string method:

let data = [
  {
    title: 'DÁKITI description:("Auto-generated by YouTube.")',
    author: 'Bad Bunny',
    duration: 205090
  }
]

let result = data.map(e => ({...e,title:e.title.replace('description:("Auto-generated by YouTube.")','').trim()}))

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

0

If you have an array with multiple objects with the auto generated description you can use forEach and update the title attribute of each element.

Why forEach is a better choice than map

forEach will not create a new array like map will do, it will simply update your existing array.

data.forEach(e => {
    e.title = e.title.replace('description:("Auto-generated by YouTube.")', '');
  })

let data = [{
  title: 'DÁKITI description:("Auto-generated by YouTube.")',
  author: 'Bad Bunny',
  duration: 205090
}]

data.forEach(e => {
  e.title = e.title.replace('description:("Auto-generated by YouTube.")', '');
})

console.log(data)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.