How to filter array of JSON-objects with multiple integer conditions and key values How to filter array of JSON-objects with multiple integer conditions and key values json json

How to filter array of JSON-objects with multiple integer conditions and key values


  1. Simplest solution: just hardcode all the fields

let boats = [  {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},  {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},  {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},  {Price: 396900, BrandName: null     , BoatYear: 2020},  {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},  {Price: 138700, BrandName: "HR"     , BoatYear: 2020},  {Price: 178900, BrandName: "HR"     , BoatYear: 2020},  {Price: 348900, BrandName: "HR"     , BoatYear: 2020},  {Price: 285800, BrandName: "HR"     , BoatYear: 2020},  {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},  {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},  {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},  {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},  {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},  {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},  {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},  {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},  {Price: 247900, BrandName: "SILVER" , BoatYear: 2020}];const expected_selected = {  BoatYear : 2020,  BrandName: 'LINDER',  Price    : { min: 0, max: 138000 },}const filter_by = filters => item => {  if (item.BoatYear === undefined || item.BoatYear !== filters.BoatYear ) return false  if (item.BrandName === undefined || item.BrandName !== filters.BrandName ) return false  if (item.Price < filters.Price.min || item.Price > filters.Price.max) return false  return true}boats = boats.filter(filter_by(expected_selected))console.log(`Results: ${JSON.stringify(boats)}`);