JavaScript JS - get array in an object by value array object value javascript,array object values,array object value

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.

Answer:1

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));
Answer:2

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>');
Answer:3
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);
Answer:4

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);
Answer:5
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
      }
    }
  }
}
Answer:6

I'm building a large AngularJS application in which some of the UI text needs to be content managed. This is because items such as contextual help will need to be edited by the client after launch in ...

I'm building a large AngularJS application in which some of the UI text needs to be content managed. This is because items such as contextual help will need to be edited by the client after launch in ...

  1. content management string

I have a list var jsonList = [ { "id": "one", "pId": "nosearch", "cId": "searchc" }, { "id": "two", "pId": "product1", "cId": "searchc" }, { "...

I have a list var jsonList = [ { "id": "one", "pId": "nosearch", "cId": "searchc" }, { "id": "two", "pId": "product1", "cId": "searchc" }, { "...

  1. json list filter
  2. json list filter jquery
  3. json list filter angularjs
  4. python json list filter
  5. javascript json list filter
  6. json drop down list filter
  7. filter json list angular

I'm working on an AngulaJS project and using the Bootstrap UI Modal window component. To close a Modal window, most examples use the following line of code: $uibModalInstance.dismiss('cancel'); I ...

I'm working on an AngulaJS project and using the Bootstrap UI Modal window component. To close a Modal window, most examples use the following line of code: $uibModalInstance.dismiss('cancel'); I ...

  1. what possible values of resultant resistance
  2. what possible values
  3. what are possible values for quantum numbers
  4. what are the possible values of remainder r
  5. what are the possible values of l for this orbital
  6. what are the possible values of ms for the orbital
  7. what are the possible values of quality gates in sonar
  8. what are the possible values of the magnetic quantum number

I am trying to build a website where a user can enter text, which will be picked up via javascript, and sent to a python function where it will be posted to twitter. For the time being, the python ...

I am trying to build a website where a user can enter text, which will be picked up via javascript, and sent to a python function where it will be posted to twitter. For the time being, the python ...

  1. sending data from arduino to raspberry pi
  2. sending data from child to parent react
  3. sending data from arduino to excel
  4. sending data from arduino to computer
  5. sending data from python to arduino
  6. sending data from raspberry pi to web server
  7. sending data from client to server
  8. sending data from activity to fragment
  9. sending data from python to html
  10. sending data from arduino to processing
  11. sending data from raspberry pi to pc
  12. sending data from microcontroller to pc
  13. sending data from javascript to python
  14. sending data from matlab to arduino
  15. sending data from arduino to esp8266
  16. sending data from android to iphone
  17. sending data from splunk to kafka
  18. sending data from plc to sql server
  19. sending data from one esp8266 to another
  20. sending data from arduino to nodemcu