i'm using vue.js. have 2 arrays, checkedtracks
, checkedalbums
. both created , populated in same way (see initialisealbums
) , both contain elements of type string
.
the updatearray
method takes array , array element parameters. if element in array, removed. if not in array, added. when call method updatearray
, pass checkedtracks
array, performs expected. when pass checkedalbums
, index -1.
while debugging method checkedalbums
noticed few things.
- the data types of element being passed in , array elements same
- the array contains data, array[0] returns undefined
- manually passing id string indexof function still returns 0, despite being identical
can provide insight going on here? in advance.
edit: have simplified code snippet include relevant information
<script> export default { data () { return { allalbums: [], checkedtracks: [], checkedalbums: [] } }, methods: { initialisealbums: function (allalbums) { let self = self.allalbums = [] self.checkedalbums = [] self.checkedtracks = [] allalbums.foreach(function (album) { if (album.tracks.items !== null) { self.allalbums.push(album) self.checkedalbums.push(album.id) album.tracks.items.foreach(function (track) { self.checkedtracks.push(track.id) }) } }) }, togglesinglealbum: function (albumid) { this.updatearray(this.checkedalbums, albumid) }, updatetrack: function (trackid) { this.updatearray(this.checkedtracks, trackid) }, updatearray: function (array, element) { let index = array.indexof(element) if (index > -1) { array.splice(index, 1) } else { array.push(element) } } } } </script>
Comments
Post a Comment