-1

I have this 2 arrays:

var a = [{cod: 123, brand: 'Audi', model: 'A3', price: 15000, km: 20000}, 
         {cod: 456, brand: 'BMW', model: 320, price: 20000, km: 30000}]
var b = [{brand: 'Audi', model: 'A3', price: 100000, km: 100000}]

What I want to do is, compare both arrays, and return the object from a that as the same brand and model as b and only if the maximum price and km don't exceed 100000. If the Audi on a had a price of 120000, then it wouldn't return anything.

EDIT: Nina answer worked perfectly. But if one of the items on filter is empty it won't return anything. How can i ignore empty values and only compare the others?

waze
  • 517
  • 1
  • 9
  • 20
  • 3
    Please include attempted solutions, why they didn't work, and the expected results. That would really help us to figure out the issue with your code. – palaѕн Dec 29 '17 at 13:55
  • `array1 === array2` to compare two arrays. – pishpish Dec 29 '17 at 13:57
  • Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – serkan Dec 29 '17 at 14:01

3 Answers3

2

You could filter the array with the filter values.

var array = [{ cod: 123, brand: 'Audi', model: 'A3', price: 15000, km: 20000 }, { cod: 456, brand: 'BMW', model: '320', price: 20000, km: 30000 }],
    filter = [{ brand: 'Audi', model: 'A3', price: 100000, km: 100000 }],
    result = array.filter(a => filter.some(b => a.brand === b.brand && a.model === b.model && a.price <= b.price && a.km <= b.km));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Im accepting this one since it was the first to comment :p it worked perfectly with that array. I just need to change one of my arrays as it is an observable array (mobX and react things) – waze Dec 29 '17 at 14:48
  • I noticed that on array filter, if any value is empty it won't compare the rest, so it won't return anything. Is there anyway to ignore null values? @Nina Scholz – waze Dec 29 '17 at 22:43
0

like this

const a = [
    {
        cod: 123,
        brand: 'Audi',
        model: 'A3',
        price: 15000,
        km: 20000,
    },
    {
        cod: 456,
        brand: 'BMW',
        model: 320,
        price: 20000,
        km: 30000,
    },
];

const b = [
    {
        brand: 'Audi',
        model: 'A3',
        price: 100000,
        km: 100000,
    },
];
function compareTwo(a, b) {
    let obj = {};
    for (let element of a) {
        if (element.model === b[0].model && element.brand === b[0].brand) {
            if (b[0].price <= 100000 && 100000 >= b[0].km)
                obj.model = element.model;
                obj.brand = element.brand;
                if(element.price === 100000) return null ;
               return obj ;
        }
    }
    return null ;
}
let myObj = compareTwo(a,b);
console.log(myObj);
mostafa tourad
  • 4,308
  • 1
  • 12
  • 21
0

Nina did the same answer as me a little faster, so here is an extended version that allows searching with dynamic keys, if it's a number the cars param needs to be less than or equal to the search param.

const cars = [
  {cod: '123', brand: 'Audi', model: 'A3', price: 15000, km: 20000},  // pass
  {cod: '123', brand: 'Audi', model: 'A3', price: 30000, km: 40000},  // pass 
  {cod: '124', brand: 'Audi', model: 'A6', price: 15000, km: 120000}, // pass 2nd query
  {cod: '125', brand: 'Audi', model: 'A8', price: 150000, km: 12000}, // fail price
  {cod: '456', brand: 'BMW', model: '320', price: 20000, km: 30000},  // fail all
]

const searches = [
  {brand: 'Audi', model: 'A3', price: 100000, km: 100000},
  {brand: 'Audi', model: 'A6'}
]

console.log(
  // filter the cars
  cars.filter(car =>
    // iterate over the searches array to check the car to the search params
    searches.some(
      // check the requirements
      search => (
        // loop over each key in the search params object
        Object.keys(search).every(
          key => isNaN(search[key])
            ? car[key] === search[key]
            : car[key] <= search[key]
        )
      )
    )
  )
)
<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>
synthet1c
  • 6,152
  • 2
  • 24
  • 39