1

How can I check if an Object Array value is null? In this case, I want to check if the price value is null

for example

available_cars:{
cars:[
{name:"BMW", price:2000000}
{name:"Volkswagen", price:1500000}
{name:"Audi", price:null}
]
}

2 Answers 2

1

Use Array.some and compare it to null:

available_cars = {
  cars: [{
    name: "BMW",
    price: 2000000
  }, {
    name: "Volkswagen",
    price: 1500000
  }, {
    name: "Audi",
    price: null
  }]
}

const res = available_cars.cars.some(e => e.price === null)
console.log(res)

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

1 Comment

I don't want to filter out the ones that have a null value. I want to check if any of them has a null value for price.
0
available_cars = {
  cars: [{
    name: "BMW",
    price: 2000000
  }, {
    name: "Volkswagen",
    price: 1500000
  }, {
    name: "Audi",
    price: null
  }]
}

const res = available_cars.cars.forEach(car => {

   if(!car.price){
// handle case
    }

});
console.log(res)

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.