-4

I have a string that I get in a jQuery callback and that looks like this 792-1816vh1,792-1816vh2 and I want to convert it to '792-1816vh1','792-1816vh2'. How can I do that? Any help appreciated, thanks.

So this is my problem:

I need to send some userid to a function, and that function is sending a push notification. And it works if the variable with the userid:s looks like "users" below, and is hardcoded in the js file. I also have two other variables with text.

var usersfromarray = strArray[2];//from a callback, not working
console.log(usersfromarray)//this writes 792-1816vh1,792-1816vh2
var users='792-1816vh1','792-1816vh2'//this is hardcoded in the js file.
var pushtext="Some text..."
var tid="now"

var str='792-1816vh1,792-1816vh2';
var res='';

str.split(',').forEach(function(elem) {
res += "'" + elem + "',";
});

// remove last ","
res = res.substring(0, res.length-1);

console.log(res);//this writes '792-1816vh1','792-1816vh2'

I send it to the function like this:

skickaPushnotiser([[pushtext,tid],users]);

If I use "users" then it is working, it sends the push.

If I use "res" then it is not working, even if it writes the same in the console as the "users".

Both "users" and "res" is writing: '792-1816vh1','792-1816vh2' in the console.

So why is "users" working and not "res"? I don´t know what Im missing? I would really appreciate some help, thanks.

Edit: I need "res" to be ["792-1816vh1", "792-1816vh2"], with the brackets and with " instead of ' Thanks.

Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • 1
    SO you want to turn it into an array? Just [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on the comma – epascarello Aug 23 '17 at 17:51
  • 1
    https://www.w3schools.com/jsref/jsref_split.asp – Bryan Dellinger Aug 23 '17 at 17:52
  • Thanks, but I need it to be with the ' character and not " as I guess it becomes with split, or? From w3school it just becomes 792-1816vh1,792-1816vh2 with a split and I need it to be with the ' character. '792-1816vh1','792-1816vh2' – Claes Gustavsson Aug 23 '17 at 17:53
  • Possible duplicate of [How do I split a string, breaking at a particular character?](https://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – The_Black_Smurf Aug 23 '17 at 17:56

1 Answers1

2

You can break it to an array using split(',') and the append it back using a loop:

var str='792-1816vh1,792-1816vh2';
var res='[';

str.split(',').forEach(function(elem) {
   res += '"' + elem + '", ';
});

// remove last ","
res = res.substring(0, res.length-2) + ']';

console.log(res);
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • Thanks Koby, great, thanks. I will test and see if it work for my specific needs. – Claes Gustavsson Aug 23 '17 at 17:59
  • How can I change it so that "res" looks like this instead ["792-1816vh1", "792-1816vh2"]. Thanks a lot Koby. – Claes Gustavsson Aug 23 '17 at 21:34
  • Thanks Koby, I now get this in the console ["792-1816vh1","792-1816vh2"] and it does´t work, but if I use the other one(hard coded) then I get this in the console and the userid:s are red and the first one is black ["792-1816vh1", "792-1816vh2"] (2), so the difference is a space after the comma and that the hardcoded one is red and it writes (2) after it as well. I have tried to get the space but with no luck. – Claes Gustavsson Aug 24 '17 at 05:02
  • Great will test. What is the difference between them? Why is one black and why i s one red? – Claes Gustavsson Aug 24 '17 at 05:04
  • @ClaesGustavsson I have no idea... I can't see what you're talking about. – Koby Douek Aug 24 '17 at 05:06
  • Ok, now I get ['792-1816vh1', '792-1816vh2',] so it is not removing the last comma, and also, can you change the ' to " :-) – Claes Gustavsson Aug 24 '17 at 05:07
  • I see in your example above that you get a space after the comma, but I don´t? I get ["792-1816vh1", "792-1816vh2"] – Claes Gustavsson Aug 24 '17 at 05:12
  • thats with a space... what you just wrote – Koby Douek Aug 24 '17 at 05:14
  • When I use your code it does´t show a space in my console, but it does in your code above. Strange. If I use this, then it works, but with yours it does´t, and I don´t understand why. var test=['792-1816vh1','792-1816vh2'] console.log(test); And with this I get this in the console. ["792-1816vh1", "792-1816vh2"] (2) – Claes Gustavsson Aug 24 '17 at 05:19
  • Now I got it working. It treated it as a array with integer values and not string values. Thanks a lot for your help Koby! – Claes Gustavsson Aug 24 '17 at 05:49