最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop through Array and return result into Array - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jul 13, 2017 at 7:18 Dino.FDino.F 911 gold badge1 silver badge10 bronze badges 4
  • when you are not doing anything with array just use same variable there.. – lalithkumar Commented Jul 13, 2017 at 7: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 Commented Jul 13, 2017 at 7:43
  • @DinoFilipovic, it is working my solution for you ? – Mihai Alexandru-Ionut Commented Jul 13, 2017 at 8:55
  • 1 @Alexandru-Ionut Mihai, yes, thank you. It worked. :) I have combined that with HPSM tools features and it worked. – Dino.F Commented Jul 13, 2017 at 10:11
Add a comment  | 

4 Answers 4

Reset to default 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);

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);

Use es6 deconstruction to spread the Array shareholders. Such as

let [user1, user2] = shareholders;// user1 equals shareholders[0] user2 equals shareholders[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.)

发布评论

评论列表(0)

  1. 暂无评论