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);
}
Chakravarthy S M
  • 608
  • 6
  • 15
user3173634
  • 87
  • 1
  • 8

3 Answers3

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"]'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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 – user3173634 Jul 31 '14 at 07:44
  • 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. – Barmar Jul 31 '14 at 07:48
  • i use it actuly rigth now, but it is to risk for use. – user3173634 Jul 31 '14 at 07:51
  • You could try `JSON.parse(value.replace(/'/g, '"'))`, but this is also risky if the string contains unescaped quotes. – Barmar Jul 31 '14 at 07:52
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
Andy Novocin
  • 454
  • 3
  • 14
  • It would be more helpful if you showed how to use this with the `data-XXX` attributes in his HTML. – Barmar Jul 31 '14 at 06:49
  • 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. – Andy Novocin Jul 31 '14 at 06:49
  • 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. – Barmar Jul 31 '14 at 06:50
  • 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. – Andy Novocin Jul 31 '14 at 06:54
  • Yeah, the title apparently led someone to flag it as a duplicate of a very different question, probably because the titles are similar. – Barmar Jul 31 '14 at 06:58
  • Maybe when the original question is edited into a more clear question my answer will become irrelevant and then I'll remove it. – Andy Novocin Jul 31 '14 at 07:02
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.
smilexu
  • 11
  • 3
  • Isn't this what I said? – Barmar Jul 31 '14 at 07:04
  • 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... – smilexu Jul 31 '14 at 07:29
  • You also lose a lot of time posting HTML instead of using the WYSIWYG editor of SO. – Barmar Jul 31 '14 at 07:31
  • 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 – user3173634 Jul 31 '14 at 07:45