4

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.

Dino.F
  • 91
  • 1
  • 1
  • 10
  • when you are not doing anything with array just use same variable there.. – lalithkumar Jul 13 '17 at 07:29
  • I need to create HTML Template for sending an e-mail to "shareholders" which are stored into "device" table. Shareholders are stored into an array of strings (characters) and I am fetching them from there to another part of the application so I can send an e-mail. – Dino.F Jul 13 '17 at 07:43
  • @DinoFilipovic, it is working my solution for you ? – Mihai Alexandru-Ionut Jul 13 '17 at 08:55
  • 1
    @Alexandru-Ionut Mihai, yes, thank you. It worked. :) I have combined that with HPSM tools features and it worked. – Dino.F Jul 13 '17 at 10:11

4 Answers4

8

Are you looking for the map function?

var shareholders = ['name1', 'name2', 'name3'];
var users = shareholders.map(function (user){
  return user; // Do transformation here
});
console.log(users);
Valentin Klinghammer
  • 1,319
  • 1
  • 13
  • 19
  • 2
    map method isn't a good choice in this situation. Usually it's used when you have to modify structure of array items. – Mihai Alexandru-Ionut Jul 13 '17 at 08:56
  • 3
    I wouldn't judge wether or not it is a good choice since it is unclear to me what the OP is actually asking. If he want's to simply copy an array (and not modify it), something like `shareholders.slice(0);` would be faster or `Array.from(shareholders);` would be more clear. – Valentin Klinghammer Jul 13 '17 at 13:06
5

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);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Since you need to fetch the array items one by one, you can use Iterator here like this:

function makeIterator(array) {
    var nextIndex = 0;

    return {
       next: function() {
           return nextIndex < array.length ?
               {value: array[nextIndex++], done: false} :
               {done: true};
       }
    };
}

Once initialized, the next() method can be called to access key-value pairs from the object in turn:

var it = makeIterator([“name1”, “name2”, “name3”];);
console.log(it.next().value); // 'name1'
console.log(it.next().value); // 'name2'
console.log(it.next().value); // 'name3'
console.log(it.next().done);  // true

you can check the details here :

https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators_and_Generators. (This example is also from the link.)

Fahad Nisar
  • 1,723
  • 12
  • 18
0

Use es6 deconstruction to spread the Array shareholders. Such as

let [user1, user2] = shareholders;// user1 equals shareholders[0] user2 equals shareholders[1].

JohnPion
  • 45
  • 1