admin管理员组文章数量:1316974
I have a rather weird issue. I have an array of html-code in string-format that I want to join together and insert into an existing element on my page.
The array looks something like this:
var sample_array = ['<div class="cld-event"><div class="cld-time">12:00</div><div class="cld-description">Some blabla</div></div>', '<div class="cld-event"><div class="cld-time">15:00</div><div class="cld-description">Some blabla</div></div>', ...];
When joining and inserting the array such as:
$(myelement).html(sample_array.join(''));
I weirdly enough get the individual html elements separated by mas, which I of course do not want.
Just logging the array join in the console also returns the elements separated by a ma. I already tried replacing the ''
argument in the join method with other strings such as '+'
, but it doesn't help either, it's always the ma that shows up.
I linted the entire code and I have no errors. Has anybody every encountered something like this? I'm sure I'm making some very obvious and very stupid mistake here, but I just can't figure it out.
I have a rather weird issue. I have an array of html-code in string-format that I want to join together and insert into an existing element on my page.
The array looks something like this:
var sample_array = ['<div class="cld-event"><div class="cld-time">12:00</div><div class="cld-description">Some blabla</div></div>', '<div class="cld-event"><div class="cld-time">15:00</div><div class="cld-description">Some blabla</div></div>', ...];
When joining and inserting the array such as:
$(myelement).html(sample_array.join(''));
I weirdly enough get the individual html elements separated by mas, which I of course do not want.
Just logging the array join in the console also returns the elements separated by a ma. I already tried replacing the ''
argument in the join method with other strings such as '+'
, but it doesn't help either, it's always the ma that shows up.
I linted the entire code and I have no errors. Has anybody every encountered something like this? I'm sure I'm making some very obvious and very stupid mistake here, but I just can't figure it out.
Share Improve this question asked Jul 19, 2011 at 8:52 ximiximi 5965 silver badges17 bronze badges 5- 1 your example works at console as expected – Molecular Man Commented Jul 19, 2011 at 8:59
- Just tried the example given on jsFiddle – Nalum Commented Jul 19, 2011 at 9:04
- I tried it in the console as well and it works, so it really is fairly weird. – ximi Commented Jul 19, 2011 at 9:11
- 7 Ok, I discovered the problem. Somewhere along the way I managed to turn the array into a two-dimensional array, thus the join behaved weirdly. Thanks to everyone that tried to help... – ximi Commented Jul 19, 2011 at 9:35
- i had the same issue. Thanks ! – Sam Commented May 18, 2022 at 9:51
4 Answers
Reset to default 3I just had this issue myself. It's four years since you asked, but maybe some people like me will Google this answer and save themselves some time.
I was working with a string that I split()
up into an array to modify some parts and then join()
it back into a string. So basically, I started with
var myNiftyStuff = 'My nifty stuff.';
myNiftyStuff = myNiftyStuff.split('');
Here, myNiftyStuff
starts life as a string. split()
then changes it into an array.
Then whatever mods I wanted to work on happened in the array. When I tried to join my array, I was calling
myNiftyStuff.join('');
Since join()
isn't destructive, it was just returning itself but not modifying the variable. But I didn't realize it, my linter wasn't barking, and nothing in the code looked off.
Then, I was passing my array to the page via jQuery (not sure if jQ matters here or not), which rendered it as a joined string using mas, which must be a default behavior.
I should have overwritten my variable like so
myNiftyStuff = myNiftyStuff.join('');
Now myNiftyStuff
is updated with join()
's return value. When I add it to the page, it's a joined string, whereas before I was passing the array without realizing I hadn't saved the result of my join()
.
I had this same problem, it seems to happen when you have a 2 levels array(one inside of another), in my case it was being caused by Object.values(myObject).
I've solved it using lodash _.flattenDeep
you can see more details in its documentation: https://lodash./docs/4.17.15#flatten
Are you sure that you are not including the mas in some way?like adding an extra set of quotes so that you only have one element of the array?
Because the only reason for this is that actually you are joining an array with only one element (so no join is performed and so join('+') is ignored)
what value has sample_array.length
?
are you sure your array is not (notice the quotes at the beginning)
["'<div class="cld-event"><div class="cld-time">12:00</div><div class="cld-description">Some blabla</div></div>', '<div class="cld-event"><div class="cld-time">15:00</div><div class="cld-description">Some blabla</div></div>', ..."];
Works for me just as you'd expect. Mind throwing together a quick jsfiddle?
However, if you get the as there, you must have them in your original array (did you split it from something else, or how did you generate the array?). Another possibility is that you might use a variable as the join argument, which somehow get's a wrong value.
You probably don't have an a-delimited array, but ["<div></div>,<div></div>"]
that looks like an array of 2 elements, but really isn't.
本文标签: jqueryJavascript join outputs string with comma despite seperator argumentStack Overflow
版权声明:本文标题:jquery - Javascript join outputs string with comma despite seperator argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742020094a2414453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论