Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue js How to split string to array and use in v-for list renderer

I want to split a row of a list and use the result in another list statement like this : I actually know that the syntax of Vue list renderer of this type is incorrect but I want to show you what I need!

var app = new Vue({
	el : "#app",
  data : {
    list : [
      {
        'name' : 'aaa',
        'codes' : '111+222+333'
      },
      {
        'name' : 'bbb',
        'codes' : '432+456+678'
      }
    ]
  }
})
<div id="app">
  <div v-for="row in list">
    <p>{{ row.name }}</p>
    <div v-for="code in (row.codes.split('+'))">
      <p>{{ code }}</p>
    <div>
  </div>
</div>

Update: The above code is correct and I had some problems when using filters like this that is wrong :

v-for="code in (row.codes | split('+'))"

like image 654
APB Avatar asked Oct 17 '25 04:10

APB


2 Answers

Here is my solution!

<div id="app">
  <div v-for="row in splitedList">
    <p>{{ row.name }}</p>
    <div v-for="code in row.codes">
      <p>{{ code }}</p>
      </div>
  </div>
</div>

new Vue({
  el: "#app",
  data: {
    list : [
      {
        'name' : 'aaa',
        'codes' : '111+222+333'
      },
      {
        'name' : 'bbb',
        'codes' : '432+456+678'
      }
    ]
  },
  computed: {
    splitedList(){
        let newArr = [...this.list]
      newArr.map(el => {
        return el.codes = el.codes.split('+')
      })
      return newArr
    }
  }

})

See it in action

like image 123
Roland Avatar answered Oct 19 '25 19:10

Roland


v-for="code in row.codes.split('+')"

like image 23
user3841678 Avatar answered Oct 19 '25 17:10

user3841678