admin管理员组文章数量:1426938
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 :)
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 :)
Share Improve this question asked Apr 25, 2018 at 12:57 Mik_AMik_A 3665 silver badges16 bronze badges 4- share any live snippet/ demo ? nothing is clear with the code you provided. – Niklesh Raut Commented Apr 25, 2018 at 13:02
-
1
you can define before function
var me= this
and insideforEach()
you can access usingme.variable
. – Narendra Jadhav Commented Apr 25, 2018 at 13:03 -
1. What
names
property isthis.names
supposed to set? 2. Note that it will get overwritten repeatedly, as you have it in theforEach
callback. 3. Note thatthis
will be eitherundefined
(strict mode) or the global object (loose mode) because you're using a traditional function as the callback and not specifying athis
valueforEach
should use. You may want an arrow function, but it's impossible to say from what you've shown. – T.J. Crowder Commented Apr 25, 2018 at 13:05 - T.J Crowder. yes I should be putting all thenames in to object value right? and then getting them each with v-for loop I'm not sure hiw to do that though (yet) – Mik_A Commented Apr 25, 2018 at 13:40
1 Answer
Reset to default 4There is two way you can access this inside another function scope (in this case forEach() ).
You can simple create a new variable referencing your scope, like
printNames: function () {
let scope = this
let tempData = JSON.parse(JSON.stringify(this.nameData))
tempData.biglist.forEach(function (nObj) {
// I can access scope here
let cName = nObj.CeName
console.log(cName) // gives long list of names
this.names = cName
})
}
, and you will have access to this variable scope inside forEach.
Or you can use arrow functions, which does not create a new scope. Therefore, same this
as outside the forEach. Here is an example:
http://jsfiddle/2eAqE/1149/
本文标签: javascriptaccess variable from within foreach loop in vuejsStack Overflow
版权声明:本文标题:javascript - access variable from within foreach loop in vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460406a2659299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论