admin管理员组文章数量:1386846
Is there a nice, clean way to concatenate a string and a variable into a variable name that Jade can understand?
Ideally, it would look something like this:
each #{shape + 'Text'} in #{shape + 'Texts'}
li #{shape + 'Text'}
I tried using window[shape + 'Text']
but that didn't seem to work. Maybe I was doing it wrong?
Here's why I want to do this:
I have an array called shapes
that looks like this: ['square', 'triangle', 'circle']
I'm using Jade's each ... in ...
function to iterate through this array. Within each iteration of my function, I need to do another each ... in ...
of one of a few other arrays. Instead of using a straight-up variable to select which array to iterate over, like each shape in shapes
, I want to concatenate shape
with a string in order to get something like each squareText in squareTexts
or each circleText in circleTexts
.
Currently, I'm using conditionals to achieve my desired result, but it's verbose and not in the minimalist spirit of the language.
Thanks in advance for any advice.
Is there a nice, clean way to concatenate a string and a variable into a variable name that Jade can understand?
Ideally, it would look something like this:
each #{shape + 'Text'} in #{shape + 'Texts'}
li #{shape + 'Text'}
I tried using window[shape + 'Text']
but that didn't seem to work. Maybe I was doing it wrong?
Here's why I want to do this:
I have an array called shapes
that looks like this: ['square', 'triangle', 'circle']
I'm using Jade's each ... in ...
function to iterate through this array. Within each iteration of my function, I need to do another each ... in ...
of one of a few other arrays. Instead of using a straight-up variable to select which array to iterate over, like each shape in shapes
, I want to concatenate shape
with a string in order to get something like each squareText in squareTexts
or each circleText in circleTexts
.
Currently, I'm using conditionals to achieve my desired result, but it's verbose and not in the minimalist spirit of the language.
Thanks in advance for any advice.
Share Improve this question edited Oct 9, 2012 at 1:48 ben asked Oct 9, 2012 at 0:01 benben 2,0472 gold badges15 silver badges16 bronze badges4 Answers
Reset to default 6So it looks like in my case, the trick is to use Javascript's eval()
function to concatenate the variable name and the string into a new variable name. Here's my successful (and succinct) implementation.
- var items = eval(shape + 'Texts');
each item, i in items
li #{items[i]}
I'd suggest looking at an option to create/eval this out of Jade, keep the jade code (and any templare for this matter) as simple as possible.
You can wrap a variable and string in parentheses.
each item, i in items
li=(shape + 'Texts')
I am not exactly following what you are referring to since I don't know jade
at all, but you would loop through an array like this.
var a = ['square', 'triangle', 'circle'];
var i, l, item;
l = a.length;
for (i = 0; i < l; i++) {
item = a[i];
// At this point, you can refer to window[item + 'Text']
// but I'm not sure what that is supposed to mean.
}
There is also an Array.prototype.every available, but I usually don't bother to monkeypatch it in to older browsers.
本文标签: javascriptConcatenate a string and a variable into a variable name for JadeStack Overflow
版权声明:本文标题:javascript - Concatenate a string and a variable into a variable name for Jade - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744572990a2613450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论