1

I am trying to replace the text "this | " with "" from the titles in an array.

What is the best way to do this?

Any help would be greatly appreciated :)

my js code:

let Feed=require('rss-to-json')
Feed.load('http://localhost:3000/news', function (err, content) {
  let appTitleList = new Vue({
    el: '#news',
    data: {
      rssItems: content.items
    },
    methods:{
      removeFunction: function () {
       this.content.title = this.content.title.replace("this | ", "");
    }
  })
})

the html:

         <div class="card" id="news">
            <ul class="list-group list-group-flush">
                <li class="list-group-item" v-for="item in rssItems" > 
                    <b>{{ item.title }}</b>
                    <p>{{ item.description }}</p>
                </li>
            </ul>
        </div>
4
  • "titles in an array" which array are you referring to here? could you please add a few sample data about how that array looks like? Also, do you want to replace it from all objects title property inside the array? Commented Apr 10, 2020 at 17:37
  • that is one item in the array, apologies for the unkind formatting Commented Apr 10, 2020 at 17:40
  • This doesn't seem like an array but more like an XML. Also, inside title there no this | but you have News24.com | instead? Commented Apr 10, 2020 at 17:42
  • yes it is xml - I have edited original question to reflect Commented Apr 10, 2020 at 17:47

2 Answers 2

1

I don't see what this.content is. I don't see where you are using removeFunction, but if you are, try this:

removeFunction: function () {
    const rssItems = [...this.rssItems]
    for (const item of rssItems) {
        item.title = item.title.replace("this | ", "");
    }
    this.rssItems = rssItems
}

Alternatively, mutate the rssItems before setting them in the state, and maybe you won't need the removeFunction.

data: {
  rssItems: content.items.map(i => ({
    ...i,
    title: i.title.replace("this | ", "")
  }))
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for this, your alternative method worked! I had to edit it slightly and use the following, data: { rssItems: content.items.map(i => ({ ...i, title: i.title.replace("News24.com | ", ""), }) )}
quick question, If there were multiple things to replace in the title, what would be the best approach using your alternative?
Add another replace: title: i.title.replace("News24.com | ", "").replace("Other Thing", "").
You are a Super Star! Have a great weekend all!
@cgd You can vote or accept an answer if it helped.
1

This can be a possible solution: fetching your API's posts when the Vue.JS instance is created, mutating the related titles and enqueue each post.

<head>
... your imports from CDN
</head>

<body>
   <div id="app">
      <div class="card" id="news">
         <ul class="list-group list-group-flush">
            <li class="list-group-item" v-for="item in items"> 
               <b>{{ item.title }}</b>
               <p>{{ item.description }}</p>
            </li>
         </ul>
      </div>
   </div>

   <script>
   new Vue({
      el: '#app',
      data () {
         return {
            items: []
         }
      },
      created () {
         Feed.load('your-api-endpoint', (err, content) => {
            // TODO: error handling...

            for (let i = 0; i < content.items.length; i++) {
               const item = content.items[i];

               // Mutate the title and enqueue the post
               item.title = item.title.replace('Title | ', '');
               this.items.push(item);
            }
         });
      }
   })
   </script>
</body>

Also, watch out that the field data in the Vue.JS instance must be a function, not an object. More about this, here.

2 Comments

thank you Federico, I couldn't get yours to work with my code, but I thank you nevertheless :)
Happy to be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.