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))
      );
2
  • You mean you want to test if key exists in obj and its also true? Commented May 15, 2019 at 13:46
  • 3
    Maybe try obj[val] instead Commented May 15, 2019 at 13:47

5 Answers 5

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));

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

1 Comment

This could be shortened to just const check = k => object[k];.
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));

Comments

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.

Comments

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.

Comments

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);

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.