using join in loops

3 views
Skip to first unread message

Alex Hough

unread,
Jun 16, 2009, 12:20:02 PM6/16/09
to TiddlyWiki
Hello there,

I would like to add commas between the fields 'primary' and
'secondary' fields.
Intuitively i tried join.sec(',') but with no success.



Below is the script.

<script>
var out = []
var tids=store.getTaggedTiddlers('Q');
for (var i=0; i<tids.length; i++) {
var prim=store.getValue(tids[i].title ,"primary");
var sec=store.getValue(tids[i].title ,"secondary");
var d='|[['+tids[i].title+']] |' +prim+'| ' +sec+'|';
out.push(d);
}
out.push('|Q|Primary|Secondary|h');
return out.join('\n');
</script>

Thanks in advance of any help
Alex

Eric Shulman

unread,
Jun 16, 2009, 1:50:24 PM6/16/09
to TiddlyWiki
> Intuitively i tried join.sec(',') but with no success.


.join('...') is a method that applies to an *array* and produces a
text string composed of all the array values concatenated together,
with the indicated text inserted between values.

For example, if
a=['foo', 'bar', 'baz']
then
a.join('mumble')
results in
'foomumblebarmumblebaz'
and
a.join(', ')
results in
'foo, bar, baz'

You can even use join to build a space-separated, bracketed list from
an array of strings, like this:

'[['+a.join(']] [[')+']]'

enjoy,
-e

alex

unread,
Jun 16, 2009, 5:42:21 PM6/16/09
to TiddlyWiki
Hi Eric,

My fields are already bracketed list. I've added then like [[tags with
spaces]] in double brackets using the FieldsEditorPlugin [1] and for
some reason outputting

var d='|[['+tids[i].title+']] |[[' +prim+']]|[[ ' +sec+']]|';

results in the wikified output of the field wrapped in Double Brackets
making a non existing tiddlylink. However

var d='|[['+tids[i].title+']] |' +prim+'| ' +sec+'|';

results in the prim and sec being wikified without the brackets.

----

sec.join(',') results in an error: "TypeError: sec.join is not a
function"
var d='|[['+tids[i].title+']] |' +prim+'| ' +sec.join(',') +'|';

I think you we hinting that I needed to make the vars *arrays' so I
have put brackets around the variables. I've uploaded a test case [2]

Big thanks for any help.

[1] http://visualtw.ouvaton.org/VisualTW.html
[2] https://files.getdropbox.com/u/1316865/slices.html


alex

unread,
Jun 17, 2009, 2:57:19 AM6/17/09
to TiddlyWiki
Eric, Ah Ah!

I can read our post as being like cryptic cross work clue.


> You can even use join to build a space-separated, bracketed list from an array of strings, like this:


I am thinking that .readBracketedList() might be useful


ALex
Reply all
Reply to author
Forward
0 new messages