Firefox 的 toSource() 函数的任何替代方案 [英] any alternative of firefox's toSource() function

查看:40
本文介绍了Firefox 的 toSource() 函数的任何替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dynarch 日历,我希望将选定的日期范围转换为字符串,就像 firefox 的 toSource() 所做的那样.

I am using dynarch calendar and I want the selected dates ranges to be converted into strings like as firefox's toSource() do.

将二维数组转换为源代码的示例:

Example of converting an two-dimensional array into source:

[20110917, [20110920, 20110922], 20110923, [20110925, 20110926]]

在其他浏览器中获得相同输出的任何替代方法..???

any alternative to get this same output in other browsers too..???

我已经在使用 jQuery 1.6.2 库.因此,我不希望使用任何其他库或更大的脚本来使此功能正常工作.

I am already using jQuery 1.6.2 library. So, I don't want any other library or bigger script to get this function to work.

推荐答案

你标记了它 已经,现代浏览器支持JSON.stringifyJSON.parse 分别用于将数据转换为文本(JSON 表示)和文本到数据的方法.

You tagged it json already, modern browsers support the JSON.stringify and JSON.parse methods for converting data to text (JSON representation) and text to data respectively.

就你而言:

var arr = [20110917, [20110920, 20110922], 20110923, [20110925, 20110926]];

// yields: [20110917,[20110920,20110922],20110923,[20110925,20110926]]
console.log(JSON.stringify(arr));

替代方法:

function arr_to_string(data) {
    if (data instanceof Array) {
        var arr = [];
        for (var i=0; i<data.length; i++) {
            arr.push(arr_to_string(data[i]));
        }
        return "[" + arr.join(",") + "]";
    }
    // Warning: we expect all array elements to be digits, do not use this if the
    // data can be a random string
    return data;
}
console.log(arr_to_string(arr));

这篇关于Firefox 的 toSource() 函数的任何替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆