开发者

conversion of string into array

in my user.data.crop_position value is "[ 100, 100, 200, 200 ]";

            var crop_position=user.data.crop_position.slice(1,user.data.crop_position.length-2);
        $('#cropbox').Jcrop({
            setSelect: crop_position,
            onChange: showPreview,
            onSelect: showPreview,
            aspectRatio: 1
        });

doing this my jcrop is not select at set postion what can i do it is due to the string i am passing in how can i remove this ,

i know this is silly question but i got these kind of proble开发者_运维百科m many time also please suggest me that in future these kind of problem dont came.

regards

rahul


The Jcrop Manual says that setSelect takes an array, not a string.

[100, 100, 200, 200] // rather than
'[100, 100, 200, 200]'

If you can't change the input format, at least you can parse it using $.parseJSON before passing it to Jcrop:

var crop_position = $.parseJSON(user.data.crop_position);

Edit: If necessary (double quotes are actually present in the string value), you can use $.parseJSON twice, first to decode the encoded string value and second to decode the array within the encoded string:

    var crop_position = $.parseJSON($.parseJSON(user.data.crop_position));

Or just strip off the surrounding double quotes before $.parseJSON:

    var crop_position = $.parseJSON(user.data.crop_position.slice(1, -1));    


setSelect - array [ x, y, x2, y2 ] Set an initial selection area

So you need an array not a string for setSelect. Why don't you make user.data.crop_position an array itself? If there is no way to change the representation you can do the conversion with a simple algorithm:

var pos  = '"[ 100, 100, 200, 200 ]"'; // user.data.crop_position
var crop_position = pos.replace(/["\[\] ]/g, '').split(',');
for (var i = crop_position.length; i--;) {
  crop_position[i] = +crop_position[i]; 
}

Now you've got an array of values instead of a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜