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!

Samir
  • 91
  • 6
  • Hope this helps.. https://codepen.io/Maniraj_Murugan/pen/VwmWgLM – Maniraj Murugan Feb 19 '21 at 06: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 – Aalexander Feb 19 '21 at 07: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: https://stackoverflow.com/questions/51667175/find-and-replace-part-of-a-property-value-in-an-array-of-objects – adiga Feb 19 '21 at 07:46
  • @adiga yes but your original duplicate wasn't appropriate for this question. *How to remove text from a string? (12 answers)* – Aalexander Feb 19 '21 at 08:08

2 Answers2

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)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
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)
Aalexander
  • 4,987
  • 3
  • 11
  • 34