动态创建子数组

时间:2012-08-12 21:55:24

标签: javascript

我有下面提到的数组。这些数组值来自Database:

如果循环找到APP字符串,那么它应该将剩余字段(包括APP值)添加到新数组。怎么做..帮助PLZ

var applications=new Array("App", "sub", "child", "App1", "sub1", "sub0", "child1", "App2", "sub2", "child2"); 

1 个答案:

答案 0 :(得分:2)

你的意思是这样吗?

var applicationArrays = [];
for (var i = 0; i < applications.length; i++) {
    if (applications[i].search("App") != -1)
        applicationArrays[applicationArrays.length] = [applications[i]];
    else
        applicationArrays[applicationArrays.length - 1].push(applications[i]);
};  

您的数组将生成以下内容:

chrome console log