0

我有一个数组(tlist),其中的键与数组链接:

tliste.push({"GROUP104":["321992","322052","321812","314022","0"]});
tliste.push({"GROUP108":["322011","322032","0"]});
tliste.push({"GROUP111":["322020","322021","322040","322041","313060","313072","0"]});

我现在需要构建一个函数来获取例如 Group104 和 Group111 的值并将它们克隆到一个新数组中:

newarrray = ["321992","322052","321812","314022","0","322020","322021","322040","322041","313060","313072","0"]

最好对新数组进行排序并删除“0”——但这并不重要。

4

1 回答 1

0

令要提取的组为 grp[]。

你可以做这样的事情 -

// Extract groups in grp[] from origArray[]
var extractGrps = function(grps, origArray) {
    var result = [];
    for(var i =0; i<grps.length; i+=1) {
        var indxInOrigArray = indexOfObjectWithKey(origArray, grps[i]);
        if(indxInOrigArray > 0) {
            var arrLocal = origArray[indxInOrigArray].grps[i];
            for(var j=0; j<arrLocal.length;j+=1)
                result.push(arrLocal[j]);
        }
    }
    return result;
}

//find Index of object in arr whose key matches the given input key
var indexOfObjectWithKey = function(arr, key) {
    for(var i=0; i<arr.length; i+=1) {
        if(arr[i].key) {
            return i;
        }
    }
return -1;
}
于 2013-07-08T11:44:19.123 回答