0

I am using Rails 4.1.6 & Ruby 2.1.5

In Javascript I took one Variable x

var x= ravi,kant,ashish

I want to show output like below

["ravi","kant","ashish"]

I write this code

var x = ravi,kant,ashish

var z = x.map(function(p){ return '"' + p + '"'; }).join(',');

If I put alert(z); the output becomes "ravi","kant","ashish" and again if I put alert(z[0]), it should come "ravi" but its coming "(double quotes)

Please suggest me how to get the output. I want answer in JS only.

1 Answer 1

1

You need to use split()

var str = "How are you doing today Ravi?";
var res = str.split(" "); // ["How", "are", "you", "doing", "today", "Ravi?"]
res[0] // "How"

Taking your example

var x= "ravi,kant,ashish";
y = x.split(","); // ["ravi","kant","ashish"]
y[0]; // "ravi"

http://www.w3schools.com/jsref/jsref_split.asp

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

il explain my quesn in detail below: i have _form.html.haml page in that i written -@insurance = InsuranceProvider.pluck(:name) =content_tag :div, class: "insu_information", data:{insurance:@insurance}do then i want dis value in application.js file for that i written varx=$('.insu_information').data('insurance'); so if i put alert(x) then value is cming ravi,kant,ashish no single quotes neither double quotes nor even bracket is cming I wana o/p ["ravi","kant","ashish"] in 1 variable.so that can use this variable onward.
Thanks..I got The solution..:):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.