构建数据库字符串

发布于 2024-10-30 16:45:14 字数 731 浏览 2 评论 0原文

我正在尝试根据网站上的一些任意数据构建一个数据库。它很复杂并且每个站点都会发生变化,因此我将省略详细信息。这基本上就是我想做的事情

function level0(arg) { textarea.innerHTML += arg + ' = {'; }
function level1(arg) { textarea.innerHTML += '\n\t' + arg + ': ['; }
function level2(arg) { textarea.innerHTML += arg + ', '; }

等等。问题是有些 level1 没有任何子级,我无法获得正确的格式。

我的三个问题如下。

  1. 结尾的逗号将在 IE 中中断(谢谢 MS)
  2. 如果没有任何子级,则不应打印空的 level1
  3. 关闭 /curly?brackets/

这是我到目前为止所拥有的演示。请注意结尾的逗号、不应打印的空 sub2 以及没有右括号或大括号

我需要重新设计整个内容吗? 还有一种方法可以将这一切都集中在一个函数中,这样我就不必担心是否添加另一层了?

编辑

这需要以字符串格式完成,我无法构建一个对象然后将其字符串化,主要是因为我需要知道我正在添加哪个元素。

I'm trying to build a database based on some arbitrary data on a website. It's complex and changes for each site so I'll spare the details. Here's basically what I'm trying to do

function level0(arg) { textarea.innerHTML += arg + ' = {'; }
function level1(arg) { textarea.innerHTML += '\n\t' + arg + ': ['; }
function level2(arg) { textarea.innerHTML += arg + ', '; }

And so on. The thing is some level1's don't have any children and I can't get the formatting right.

My three problems are as follows.

  1. The ending commas are going to break in IE (thank you MS)
  2. Empty level1's shouldn't be printed if they don't have any children
  3. Closing /curly?brackets/

HERE'S A DEMO of what I have so far. Notice the ending commas, the empty sub2 which shouldn't be printed, and no closing brackets or braces

Do I need to redesign the entire thing?
Is there also a way to have this all in one function so I don't have to worry if I add another layer?

EDIT

This needs to be done in a string format, I can't build an object and then stringify it, mostly because I need to know which element I'm in the middle of adding to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-11-06 16:45:14

总的来说,看起来您仍然可能想要构建一个对象,但如果您坚持不构建它 - 这里有一些示例解决方案:

function Printer() {
    var result = '',
        lastLevel = null,
        close = {0:'\n}', 1:']', 2:''},
        delimiter = {0: ',\n', 1:',\n', 2:','};

    function closeLevel(level, noDelimiter) {
        if(lastLevel === null)
            return;
        var l = lastLevel, d = level == lastLevel;        
        while(l >= level) {
            result += close[l] + (l == level && !noDelimiter ? delimiter[l]:'');
            l--;
        }
    }

    this.level0 = function(arg) {
        closeLevel(0);
        result += arg + ' = {\n';
        lastLevel = 0;
    };

    this.level1 = function(arg) {
        closeLevel(1);
        result += '\t' + arg + ': [';
        lastLevel = 1;
    };

    this.level2 = function(arg) {
        closeLevel(2);
        result += arg;
        lastLevel = 2;
    };

    this.getResult = function() {
        closeLevel(lastLevel, true);
        return result;
    }
}



var p = new Printer();
p.level0('head');

p.level1('sub1');
p.level2('item1');p.level2('item2');p.level2('item3');
p.level1('sub2');
p.level1('sub3');
p.level2('newthing');
p.level0('head2');

document.getElementById('textarea').value = p.getResult();

您可以看到它的实际效果 这里

Overall it looks that you still might want to build an object, but in case you insist on not building it - here is some sample solution:

function Printer() {
    var result = '',
        lastLevel = null,
        close = {0:'\n}', 1:']', 2:''},
        delimiter = {0: ',\n', 1:',\n', 2:','};

    function closeLevel(level, noDelimiter) {
        if(lastLevel === null)
            return;
        var l = lastLevel, d = level == lastLevel;        
        while(l >= level) {
            result += close[l] + (l == level && !noDelimiter ? delimiter[l]:'');
            l--;
        }
    }

    this.level0 = function(arg) {
        closeLevel(0);
        result += arg + ' = {\n';
        lastLevel = 0;
    };

    this.level1 = function(arg) {
        closeLevel(1);
        result += '\t' + arg + ': [';
        lastLevel = 1;
    };

    this.level2 = function(arg) {
        closeLevel(2);
        result += arg;
        lastLevel = 2;
    };

    this.getResult = function() {
        closeLevel(lastLevel, true);
        return result;
    }
}



var p = new Printer();
p.level0('head');

p.level1('sub1');
p.level2('item1');p.level2('item2');p.level2('item3');
p.level1('sub2');
p.level1('sub3');
p.level2('newthing');
p.level0('head2');

document.getElementById('textarea').value = p.getResult();

You could see it in action here.

等待我真够勒 2024-11-06 16:45:14

我不知道为什么你要使用字符串连接来构建看起来像带有嵌套数组的对象。像这样的事情会简单得多,因为它不需要修复尾随逗号等:

编辑:我已经更新了代码以使其跟踪放入的最后一个级别。

function Db() {
    var level0, level1;
    var data = new Object();

    this.level0 = function(arg) {
        level0 = new Object();
        data[arg] = level0;

    }
    this.level1 = function(arg) {
        level1 = new Array();
        level0[arg] = level1;
    }
    this.level2 = function(arg) {
        level1.push(arg);
    }

    this.toString = function() {
        var s = '';
        for(i in data) {
            s += i + '\n';
            for(j in data[i]) {
                if(data[i][j].length>0) {
                    s += '\t' + j + ': [' + data[i][j] + ']\n' ;
                }
            }
        }
        return s;
    }

}

像这样使用:

var db = new Db();
db.level0('head');
db.level1('sub1');
db.level2('item1');db.level2('item2');db.level2('item3');

我已经在你链接的演示,它工作得很好。

I'm not sure why you're building what looks like objects with nested arrays, using string concatenation. Something like this would be much simpler, since it wouldn't require fixing trailing commas, etc:

Edit: I've updated the code to make it keep track of the last level put in.

function Db() {
    var level0, level1;
    var data = new Object();

    this.level0 = function(arg) {
        level0 = new Object();
        data[arg] = level0;

    }
    this.level1 = function(arg) {
        level1 = new Array();
        level0[arg] = level1;
    }
    this.level2 = function(arg) {
        level1.push(arg);
    }

    this.toString = function() {
        var s = '';
        for(i in data) {
            s += i + '\n';
            for(j in data[i]) {
                if(data[i][j].length>0) {
                    s += '\t' + j + ': [' + data[i][j] + ']\n' ;
                }
            }
        }
        return s;
    }

}

Use like this:

var db = new Db();
db.level0('head');
db.level1('sub1');
db.level2('item1');db.level2('item2');db.level2('item3');

I've tested this in the demo you linked and it works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文