0

Consider the following object

let obj = {
   1:true,
   2:false,
   3:true
}

and some value like let val = 1; or 2, whatever.

I want to get either true or false

In our example only for 1 and 3 it should return true. I already tried this solution but when i test for 2 it still returns true when it should return false

const isFavourited =
      (Object.keys(obj).some(id => val == id &&
        Object.values(obj).some(value => value == true))
      );
Chris
  • 155
  • 4
  • 11

5 Answers5

3

You could take a function which checks the value.

const check = k => object[k] === true;

var object = { 1: true, 2: false, 3: true };

console.log(check(1));
console.log(check(2));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

let obj = {
   1:true,
   2:false,
   3:true
}

var val1 = 1,
    val2 = 2,
    val3 = 3,
    val4 = 4

function checkValue(key){
  return !!obj[key];
}
console.log(checkValue(val1));
console.log(checkValue(val2));
console.log(checkValue(val3));
console.log(checkValue(val4));
sarvon ks
  • 626
  • 3
  • 10
0

The array function some returns true if one of the elements passes the check.

Object.values(obj).some(value => value == true))

When you are checking for the value here you're not specifying the id (the previous check for id is seperate). Therefore it checks all the elements of the array until one passes the check (index 0 will pass the check).

I recommend using Nina's answer instead.

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
Marwane Boudriga
  • 181
  • 1
  • 12
0

When you do the following

const isFavourited =
      (Object.keys(obj).some(id => val == id &&
        Object.values(obj).some(value => value == true))
      );

You are not checking if the value of a particular index is true or false. Here is what actually happens when you have val = 1:

  • the first .some will iterate over all keys
  • val == id will be true for the second key which means:
  • the Object.values(obj).some(value => value == true) will be executed
  • it returns true because there is at least one value that is true in the entire obj. The result of this operation are not dependant on the value of the key - only whether it runs or not.

let obj = { 1:true, 2:false, 3:true }

let check = Object.values(obj).some(value => value == true)

console.log(check);

So, the algorithm checks if obj contains a given key and any value that is true, as opposed to if the value for a given key is true.

You do not need to loop over the object to verify this - fetching the value of obj[val] is all you need - if the key val doesn't exist, you'd get undefined back, otherwise you'll get the value. So you merely need to check if the value you got back is true or not:

let obj = { 1:true, 2:false, 3:true }

console.log(1,  obj[1]  == true);
console.log(2,  obj[2]  == true);
console.log(3,  obj[3]  == true);
console.log(42, obj[42] == true);

This can be further shortened to !!obj[index] using implicit casting to boolean.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
0
let obj = { 1:true, 2:false, 3:true };
//Create an temp object
let tempObj = {};

//Loop over keys and filter if key value is true and add to temp object
Object.keys(obj).filter((o)=> {if(obj[o]) tempObj[o] = obj[o]});

console.log(tempObj);