如何通过数组键值将数组分成两个数组

时间:2014-04-28 01:42:52

标签: jquery

我将两个差异数组合并到一个数组中并在jQuery中返回数组。

这是我收到的结果:

[{"BRAND":"OHTSU FALKEN"},
{"BRAND":"OHTSU FALKEN INDO"},
{"PATTERNS":"FKU"},
{"PATTERNS":"SN807"},
{"PATTERNS":"SN816"},
{"PATTERNS":"SN828"},
{"PATTERNS":"ZE912"}]

如何通过识别键值来将此数组拆分为BRAND数组和PATTERNS数组?

我发现大多数asnwer正在给出循环每个数组的解决方案,只得到密钥0,1,2,3,4,5,6,7,8,9 ...

我怎么能这样:

if(key_value == 'BRAND')
{
//put into BRAND array
}else if(key_value == 'PATTERNS')
{
//put into PATTERNS array
}

1 个答案:

答案 0 :(得分:0)

编辑:我道歉,误解了结构。

var combinedArray = [{"BRAND":"OHTSU FALKEN"},
    {"BRAND":"OHTSU FALKEN INDO"},
    {"PATTERNS":"|FKU"},
    {"PATTERNS":"|SN807"},
    {"PATTERNS":"|SN816"},
    {"PATTERNS":"|SN828"},
    {"PATTERNS":"|ZE912"}];

var brandArray = [];
var patternsArray = [];

for (var i = 0; i < combinedArray.length; i++) {
    if (combinedArray[i].BRAND) {
        brandArray.push(combinedArray[i].BRAND);
    } else if (combinedArray[i].PATTERNS) {
        patternsArray.push(combinedArray[i].PATTERNS);
    }
}