结合JSON数组 [英] Combining JSON Arrays

查看:120
本文介绍了结合JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个json数组,每个数组以相同的格式列出信息:

I have 3 json arrays, each with information listed in the same format:

Array:
    ID:
    NAME:
    DATA:

    ID:
    NAME:
    DATA:

    etc...

我的目标是将所有3个数组组合为一个数组,并通过将3个数组传递给函数来按名称进行排序和显示.

My goal is to combine all 3 arrays into one array, and sort and display by NAME by passing the 3 arrays into a function.

我尝试过的功能是:

JavaScript调用:

Javascript Call:

// to save time I'm just passing the name of the array, I've tried passing
// the full array name as json[0]['DATA'][array_1][0]['NAME'] as well.

combineNames(['array_1','array_2']);

功能:

function combineNames(names) {

    var allNames = []

    for (i=0;i<names.length;i++) {
        for (j=0;j<json[0]['DATA'][names[i]].length;j++) {
            allNames.push(json[0]['DATA'][names[i]][j]['NAME']);
        }
    }

    return allNames.sort();
}

上面的错误提示我,NAME为空或未定义.

The above gives me the error that NAME is null or undefined.

我还尝试使用array.concat函数,该函数在我对其进行硬编码时会起作用:

I've also tried using the array.concat function which works when I hard code it:

var names = [];
var allNames = [];

var names = names.concat(json[0]['DATA']['array_1'],json[0]['DATA']['array_2']);

for (i=0;i<names.length;i++) {
    allNames.push(names[i]['NAME']);
}

return allNames.sort();

但是我无法弄清楚如何将数组传递给函数(如果可能的话,我只想传递数组名称部分而不是整个json [0] ['DATA'] ['array_name ']就像我在第一个功能中尝试做的那样...

But I can't figure out how to pass in the arrays into the function (and if possible I would like to just pass in the array name part instead of the whole json[0]['DATA']['array_name'] like I was trying to do in the first function...

推荐答案

如果您有3个这样的数组:

If you've got 3 arrays like this:

[{ "id":1, "name":"Bob", "data":1},{ "id":2, "name":"Fred", "data":2 }]

只需:

function combine() {
    var ar = [];

    return ar.concat.apply(ar, arguments).sort(function (a, b) {
        var aName = a.NAME;
        var bName = b.NAME;
        if (aName < bName) {
            return -1;
        } else if (aName == bName) {
            return 0;
        } else {
            return 1;
        };
    });
};

然后将其命名为:

var jointArrays = combine(array1, array2, array3, ...);

但是,如果您的JSON如下所示:

However, if your JSON looks like this:

json[0]['DATA'][array_1]
json[0]['DATA'][array_2]
json[0]['DATA'][array_3]

您可以按如下所示简单地定义combine(),这将更加方便:

You can simply define combine() as follows, which will be more convenient:

function combine(arrays) {
    var ar = [];

    return ar.concat.apply(ar, arrays).sort(function (a, b) {
        var aName = a.NAME;
        var bName = b.NAME;
        if (aName < bName) {
            return -1;
        } else if (aName == bName) {
            return 0;
        } else {
            return 1;
        };
    });
};

然后将其命名为:

var jointArrays = combine(json[0].DATA);

如果只需要名称而不是对象的数组,请使用以下命令:

If you're wanting an array of just the names, rather than the objects, use the following:

function combine(arrays) {
    var ar = [],
        ret = [];

    ar = ar.concat.apply(ar, arrays);

    for (var i=0;i<ar.length;i++) {
        ret.push(ar.NAME);
    };

    return ret.sort();
};

JavaScript区分大小写;确保它是DATA而不是dataNAME而不是name.

Javascript is case sensitive; make sure it's DATA and not data, and NAME and not name.

现在要做一些家务.

在您的示例中,两个计数器变量都被声明为隐式全局变量",因为您没有在它们前面加上var语句(

In your example, both of your counter variables are being declared as "implied globals", because you're not prefixing them with the var statement (and implied globals are bad). You should use:

for (var i=0;i<something.length;i++) {
   //
};

而不是忽略var.

此外,"{}"创建一个对象. "[]"创建一个数组. Javascript不支持关联数组;例如,键为数字的数组,数字除外.您的JSON返回的是对象数组

Also, "{}" creates an object. "[]" creates an array. Javascript does not support associative array's; e.g array's with keys that are anything except a number. What you're JSON is returning is an array of objects

方符号"和点符号"可以互换. object["one"]等同于object.one

"Square notation" and "dot notation" are interchangeable. object["one"] is equivalent to object.one

当键存储为变量或访问数组时,通常使用平方表示法.

Square notation is generally used when the key is stored as a variable, or when you're accessing an array.

var key = "one";
object[key]

希望这会有所帮助.

这篇关于结合JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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