javascriptarrayshp-service-manager

Loop through Array and return result into Array


I have this array:

var shareholders = [“name1”, “name2”, “name3”];

This is function from HPSM that is fetching data from that array:

function getShareholders(RECORD)
 {
  var fShareholder = new SCFile("device");
  var rc = fShareholder.doSelect("logical.name=\"" + RECORD + "\"");
  if (rc == RC_SUCCESS)
  {
    print(fShareholder.shareholder_contacts);
    return fShareholder.sharholder_contacts;
    }
 return null;
}

It returns them in array form but I need it to fetch one by one:

var users = new Array();
users[0] = “name1”
users[1] = “name2”
….

I have tried them to loop through for loop but without success.


Solution

  • You can use forEach function, which accepts a callback function.

    forEach method executes a provided function once for each array element.

    Syntax:

    arr.forEach(function callback(currentValue, index, array) {
    
    }[, thisArg]);
    

    var shareholders = ['name1', 'name2', 'name3'];
    var users=new Array();
    shareholders.forEach(function(item,i){
      users[i]=item;
    });
    console.log(users);