how do I access this.variable from a foreach loop?
I have like this
<template><div><li>{{ names }}</li></div></template>
var initData = {
names: '',
}
}
export default {
data: function () {
return initData
},
props: ['nameData'],
methods: {
printNames: function () {
let tempData = JSON.parse(JSON.stringify(this.nameData))
tempData.biglist.forEach(function (nObj) {
let cName = nObj.CeName
console.log(cName) // gives long list of names
this.names = cName
})
}
},
So what I want is to have the names in my list. Thanks people :)
var me= thisand insideforEach()you can access usingme.variable.namesproperty isthis.namessupposed to set? 2. Note that it will get overwritten repeatedly, as you have it in theforEachcallback. 3. Note thatthiswill be eitherundefined(strict mode) or the global object (loose mode) because you're using a traditional function as the callback and not specifying athisvalueforEachshould use. You may want an arrow function, but it's impossible to say from what you've shown.