Samuel Sharpe
Samuel Sharpe

Reputation: 43

Check if an object array value is null

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}
]
}

Upvotes: 1

Views: 72

Answers (2)

Spectric
Spectric

Reputation: 31987

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)

Upvotes: 1

theChoosyBeggar
theChoosyBeggar

Reputation: 332

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)

Upvotes: 0

Related Questions