2

I have an object game = {}; and I save an array with two id's under every number of the object - game[number] = [id1, id2]; e.g. game[0] = [234342, 134313]. How can I get the number of the object game[number] by only ONE id of the array [id1, id2]? Obviously every id is only used once in the whole object.

Thanks for your time.

1
  • why not use an array? or better why do you use an object if you use an array structure? Commented May 3, 2016 at 17:43

6 Answers 6

2

What about something like this? for each item in object, for each item in array of ids, look for our id.

var game = {
    1:[123,345],
    2:[124,456],
    3:[125,567]
}

function find(findId) {
    var returnId = 0;
    for(var id in game) { 
        console.log(game[id]); 
        for (var i = 0; i < game[id].length; i++) {
            if (game[id][i] == findId) {
                returnId = id;
            }
        }
    }
    return returnId;
}

console.log(find(124));
Sign up to request clarification or add additional context in comments.

Comments

1

You could use Array#some() with the possibillity of short circuit if the id is found.

var game = { 1: [123, 345], 2: [124, 456], 3: [125, 567] },
    ids = function (id, data) {
        var r;
        Object.keys(data).some(function (k) {
            return data[k].some(function (a) {
                if (id === a) {
                    r = data[k];
                    return true;
                }
            });
        });
        return r;
    }(456, game);

document.write('<pre>' + JSON.stringify(ids, 0, 4) + '</pre>');

Comments

1
var game = { '0': [ 234342, 134313 ],
  '1': [ 23434, 13431 ],
  '2': [ 23432, 13433 ] }

var i
var id = 23432;
for (i in game){
    if (game[i].indexOf(id) > -1){
        console.log(i);
        break;
    }
}

Or function based approach

var game = { '0': [ 234342, 134313 ],
  '1': [ 23434, 13431 ],
  '2': [ 23432, 13433 ] }

function getNumber(id, game){
  var i
  for (i in game){
    if (game[i].indexOf(id) > -1){
        return i;
    }
  }
}

var id = 23432;
var number = getNumber(id, game)
console.log(number);

Comments

1

You can use filter and reduce for that

function findIt(game, theId){
  return Object.keys(game).filter(function(index){
    return game[index].reduce((a, b) => (a == theId || b == theId));
  })[0];
}

findIt(game, 444);

Comments

1
var game, id, k, val;
game = {
  1: ['aa', 'ab'],
  2: ['ba', 'bb']
};
find_game = function(id, data) {
  for (k in game) {
    for (val in game[k]) {
      if (id === game[k][val]) {
        return k
      }
    }
  }
}

1 Comment

Improve your answer with output raw.
0

You are anyways using an object. Use your id's array as key. Use something like this, game[[id1,id2]] = number. This shall give you a faster lookup because, JS objects are hash data structures. Your method gives O(n) lookup, while hash objects are generally meant to give O(logn) time.

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.