-5

I try like this :

<script type="text/javascript">
    var clubs = [ 
        {id: 1, name : 'chelsea'},
        {id: 2, name : 'city'},
        {id: 3, name : 'liverpool'},
        {id: 4, name : 'manchester united'},
        {id: 5, name : 'arsenal'}
    ];
    var selectedId = 3;
    if(clubs.includes(selectedId))
        console.log('The selected id exist');
    else
        console.log('The selected id does not exist');
</script>

But it does not works. I only works if clubs array is array one dimensional

How can I solve it?

I want to do it with one statement. No loop

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • 1
    You can use `some()` method as suggested in your [previous question](https://stackoverflow.com/questions/49769220/how-can-i-add-object-to-array-by-certain-condition) a few minutes ago. – Mohammad Usman Apr 11 '18 at 08:49
  • 1
    `clubs.some(o => o.id == selectedId)` – Mohammad Usman Apr 11 '18 at 08:52
  • 2
    It doesn't work because there's no `3` in your `clubs` array. There's a `{id: 3, name: 'liverpool'}` object though. – Aaron Apr 11 '18 at 08:52
  • Like @Aaron said, there's no `3` in your array. Javascript doesn't know that it should map `3` to `id` unless you tell it to do so as suggested by @Mohammed-Usman – H77 Apr 11 '18 at 08:57
  • 2
    Just wanted to point out: clubs array *is* one dimensional. It's an array of objects. – freedomn-m Apr 11 '18 at 09:07

3 Answers3

1

See if this helps.

var clubs = [{id: 1, name: 'chelsea'},
     {id: 2, name: 'city'},
     {id: 3, name: 'liverpool'},
     {id: 4, name: 'manchester united'},
     {id: 5, name: 'arsenal'}];

   var selectedId = 3;

   var exist = clubs.some(function(obj) { 
     return obj.id === selectedId;
   });

   if(exist) {
     console.log("Exist");
   }
   else{
     console.log("Not Exist");
   }
Hrishikesh
  • 632
  • 3
  • 13
0

You can solve this with .some() as Mohammad pointed out, but you need to pass it a function so that it can iterate through each object(some() and includes() are looping functions by the way):

https://jsfiddle.net/bdju9gLd/

var clubs = [ 
    {id: 1, name : 'chelsea'},
    {id: 2, name : 'city'},
    {id: 3, name : 'liverpool'},
    {id: 4, name : 'manchester united'},
    {id: 5, name : 'arsenal'}
];
var selectedId = 3;
if(clubs.some(function(obj) { return obj.id == selectedId; }))
    $("#text").text('The selected id exist');
else
    $("#text").text('The selected id does not exist');
RoboYak
  • 1,352
  • 11
  • 15
0

I think you have to loop if you want to keep your current structure. Anyway if you can make this little change i'd suggest to add the id as the key of the obj

var clubs = {
    4: {id: 1, name : 'chelsea'},
    3: {id: 3, name : 'city'},
};
var selectedId = 3;
if(selectedId in clubs) alert('yes')

as you can see from the example above, in this case an alert will display, cause it will find the obj with 3 as key.

var clubs = {
    4: {id: 1, name : 'city0'},
    8: {id: 8, name : 'city1'},
    9: {id: 9, name : 'city2'},
    10: {id: 10, name : 'city3'},
    11: {id: 11, name : 'city4'},
};
var selectedId = 3;
if(selectedId in clubs) alert('yes')

in this case, nothing will display cause there is no key 3

it's a possible solution if you can edit the structure of clubs.

bLuke
  • 174
  • 1
  • 8