1

I have a JavaScript variable with comma separated string values - i.e. value1,value2,value3, ......,valueX,

I need to convert this variable's values into a JSON object. I will then use this object to match user enteredText value by using filterObj.hasOwnProperty(search)

Please help me to sort this out.

Simon Adcock
3,5603 gold badges28 silver badges42 bronze badges
asked Mar 27, 2013 at 8:11
2
  • 3
    As there is no such thing as a "Json object", it's hard to get what you want. Maybe you should look at what is JSON ? Commented Mar 27, 2013 at 8:11
  • You can match entered text by just doing str.indexOf(search) != -1 Commented Mar 27, 2013 at 8:22

4 Answers 4

3

What you seem to want is to build, from your string, a JavaScript object that would act as a map so that you can efficiently test what values are inside.

You can do it like this :

var str = 'value1,value2,value3,valueX';
var map = {};
var tokens = str.split(',');
for (var i=tokens.length; i--;) map[tokens[i]]=true;

Then you can test if a value is present like this :

if (map[someWord]) {
 // yes it's present
}
answered Mar 27, 2013 at 8:24
Sign up to request clarification or add additional context in comments.

3 Comments

thank you guys. You really save my click. Your solutions are working for me. thank you again.
Dude. for (var i = tokens.length; i--;), really? Even while is more readable.
Well. It saved a click. You can't argue against that.
2

Why JSON? You can convert it into an array with split(",").

var csv = 'value1,value2,value3';
var array = csv.split(",");
console.log(array); // ["value1", "value2", "value3"]

Accessing it with array[i] should do the job.

for (var i = 0; i < array.length; i++) {
 // do anything you want with array[i]
}

JSON is used for data interchanging. Unless you would like to communicate with other languages or pass some data along, there is no need for JSON when you are processing with JavaScript on a single page.

answered Mar 27, 2013 at 8:14

Comments

0

JavaScript has JSON.stringify() method to convert an object into JSON string and similarly JSON.parse() to convert it back. Read more about it

All about JSON : Why & How

Cheers!!

answered Jul 4, 2016 at 18:16

Comments

-1

JSON format requires (single or multi-dimensional) list of key, value pairs. You cannot just convert a comma separated list in to JSON format. You need keys to assign.

Example,

[
 {"key":"value1"},
 {"key":"value2"},
 {"key":"value3"},
 ...
 {"key":"valueX"}
]

I think for your requirement, you can use Array.

answered Mar 27, 2013 at 9:19

1 Comment

["value1", "value2", "value3"] is a valid JSON array

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.