0

In my markup i got this code

<div class="slider-rammi" data-slider-width="800" data-slider-nav="true" data-slider-smart-speed="1000" data-slider-nav-Class="['forrige', 'næste']">

the trouble is when I console.log the typeof It says it is a string fair enough, but i need to convert it to an array.

I have tried so many things but nothing really work, hope you guys can help me,

Console.log result

800 "number"
width

true "boolean"
nav

1000 "number"
smartSpeed

['forrige', 'næste'] string
navClass

the javascript right now

var data_options_control = function(scope){
    var obj = {};

    $.map(scope.$element.data(), function (value, key) { 
        var new_name = key.replace("slider", "");

        console.log(value, typeof value)

        if(typeof value == "number")
        {
            var obj_value = value;
        }
        else if(typeof value == "string") {
            if(value.charAt(0) === "[" || value.charAt(0) === "{") {
                //var obj_value = JSON.parse(value);
                console.log(obj_value)
            }
            else
            {
                var obj_value = value;
            }
        }
        else if(typeof value == "Boolean") {
            var obj_value = value;
        }
        else if(typeof value == "object") {
            var obj_value = value;
        }

        if(new_name.substring(0,3) != "Url") {
            var new_lowercase_name = new_name.substring(0,1).toLowerCase();
            var lowercase_name = new_lowercase_name + new_name.substring(1,new_name.length);        
        }
        else
        {
            var new_lowercase_name = new_name.substring(0,3).toUpperCase();
            var lowercase_name = new_lowercase_name + new_name.substring(3,new_name.length);
        }

        console.log(lowercase_name)

        obj[lowercase_name] = obj_value;


    });


    scope.options = $.extend({}, scope.options, obj);

    console.log(scope.options);
}
1
  • This question lacks a clear problem statement. Commented Jul 31, 2014 at 8:20

3 Answers 3

1

From the jQuery documentation:

When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.

In JSON, strings must be enclosed in double quotes, not single quotes, so you need to do:

data-slider-nav-Class='["forrige", "næste"]'
Sign up to request clarification or add additional context in comments.

4 Comments

the problem is i have not control how it will come out, so i need to find a way to awnser that question on with out change enything in html
Then you need to write your own parser, because jQuery will only parse JSON automatically. If you're really desperate you could use eval(), but don't say you got that advice from me.
i use it actuly rigth now, but it is to risk for use.
You could try JSON.parse(value.replace(/'/g, '"')), but this is also risky if the string contains unescaped quotes.
0

Use JSON.parse

Here is a working fiddle:

http://jsfiddle.net/gwZgS/

var stringArray = "[\"item1\", \"item2\"]";
var arrayArray = JSON.parse(stringArray);
alert(arrayArray.length); //should give 2
alert(stringArray.length); //should give 18

6 Comments

It would be more helpful if you showed how to use this with the data-XXX attributes in his HTML.
I voted yours up when I saw it. The escape of double quotes in double quotes and JSON.parse are interesting enough to leave it be I think.
JSON.parse won't work unless he modifies his HTML as I showed. And if he does that, he doesn't need to call it, because jQuery will do it automatically.
Understood, your answer is fine and probably better for the OP. However, the title of the question will lead other types of people here looking to convert a string into an array, and even the OP goes about hacking at his data. Not trying to steal anything from you, just trying to add some minor value.
Yeah, the title apparently led someone to flag it as a duplicate of a very different question, probably because the titles are similar.
|
0

Change

data-slider-nav-Class="['forrige', 'næste']"
to
data-slider-nav-Class='["forrige", "næste"]'

See the difference?Pay attention to Quotation marks !

Then you can use

$('.slider-rammi').data('sliderNavClass')
to get an array object.

4 Comments

Isn't this what I said?
yes, when i was answering this question, your answer didn't post yet.Because of my poor English , I lost a lot of time to translate word when answering this question...
You also lose a lot of time posting HTML instead of using the WYSIWYG editor of SO.
the problem is i have not control how it will come out, so i need to find a way to awnser that question on with out change enything in html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.