2

I have an array players[]

function that gets certain object from such array by looking up its gameSocketId value and returns that object

getUserInfo : function(user)
        {
            var userInfo = Game.players.filter(function(e) {
                return e.gameSocketId === user;
            }).pop();

            return userInfo;
        }

so I store it in a variable like var user = getUserInfo(userId) How can I than find out what is the position of user in array of players[] knowing all info about it?

0

3 Answers 3

4

Use .findIndex:

getUserInfo : function(user)
    {
        var userInfoIndex = Game.players.findIndex(function(e) {
            return e.gameSocketId === user;
        });

        return userInfoIndex;
    }

Note that .findIndex, while fully specified is not included in most JS engines by default yet - there is a polyfill on mdn:

if (!Array.prototype.findIndex) {
  Array.prototype.findIndex = function(predicate) {
    if (this == null) {
      throw new TypeError('Array.prototype.findIndex called on null or undefined');
    }
    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }
    var list = Object(this);
    var length = list.length >>> 0;
    var thisArg = arguments[1];
    var value;

    for (var i = 0; i < length; i++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        return i;
      }
    }
    return -1;
  };
}

This polyfill works on ES3 and ES5 browsers just fine :)

Of course, one can also use a normal for loop to do this which works all the way through ES1 - but then you don't get the fun syntax that conveys intent pretty clearly:

getUserInfo : function(user) {
    for(var i = 0; i < Game.players.length; i++){
        if(Game.players[i].gameSocketId === user) return i;
    }
    return -1;
}

We don't always have to be clever :) Of course, we can also always be inefficient and just call .indexOf after obtaining the item using your original method.

Sign up to request clarification or add additional context in comments.

Comments

1

The second param of Array.filter is the index for the current item. The below will still return you the userInfo that you originally specified plus you can use the index for whatever you want.

    getUserInfo : function(user)
    {
       var playerIndex;
        var userInfo = Game.players.filter(function(e, index) {
            if (e.gameSocketId === user) {
                playerIndex = index;
                return true;
            }
        }).pop();

        console.log(Game.players[playerIndex]) // <- the player that is also "user"

        return userInfo;
    }

Comments

0

How about using indexof()

getUserInfo : function(user){
    var userInfo = Game.players.filter(function(e) {
        return e.gameSocketId === user;
    }).pop();

    return userInfo;
}

// and later
var user = getUserInfo(userId)
console.log(Game.players.indexOf(user));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.