Compare with if on objects of JS objects

0

I need some help but I just can not find a way to solve that little doubt I have, I'm trying to make a comparison to get true / false from an Object in JavaScript, for example I have that result in the Chrome console:

{sortBy: "price_asc"}

Taking into account that the variable is an object but how can I get from that variable that the value is "price_asc" = true , try with Object.keys () but returns me ["sortBy"]

There is some way to get in the if for example:

if (Object.keys(this.searchParams) === 'price_asc') {
    return null;
}

It is appreciated if someone can clarify that doubt.

    
asked by Marco Montoya 04.09.2018 в 19:28
source

1 answer

4

You can do so using the . :

var obj = {sortBy: "price_asc"}

if(obj.sortBy === "price_asc"){
console.log("si, es price_asc")
}

or using []

var obj = {sortBy: "price_asc"}

var key = "sortBy"

    if(obj[key] === "price_asc"){
    console.log("si, es price_asc")
    }

or using Object.values

var obj = {sortBy: "price_asc"}

if(Object.values(obj)[0] === "price_asc"){
console.log("si, es price_asc")
}

If you want to iterate, you can use for in :

var obj = {sortBy: "price_asc"}

for(key in obj){
    console.log(key)
    console.log(obj[key])
}

if the object is this.searchParams would be this.searchParams.sortBy

    
answered by 04.09.2018 / 19:31
source