0

I have an array (tlist) with keys linked with arrays:

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

I now need to build a function to take the values of e.g. Group104 and Group111 and clone these into a new array:

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

Preferably the new array should be ordered and the "0" should be removed - but that is of lower importance.

1 Answers1

0

Let the groups to be extracted be grp[].

You can do something like this -

// 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;
}
abipc
  • 997
  • 2
  • 13
  • 35