Boky
Boky

Reputation: 12064

Filter through an array in javascript

I have one array with a list of all cars, as follows :

const allCars = [
       {id: 1, listID: 1, name: "Car 1", chassis: "000000000"},
       {id: 2, listID: 2, name: "Car 2", chassis: "111111111"},
       {id: 3, listID: 1, name: "Car 3", chassis: "222222222"},
       {id: 4, listID: 1, name: "Car 4", chassis: "333333333"}
    ];

and I have some of those cars in the shopping cart, as follows :

const carsInCart = [
           {carID: 1, listID: 1, offer: 488},
           {carID: 2, listID: 2, offer: 786},
        ]

Is there any way to filter throught allCars and return only those which are in carsInCart, and return only chassis from allCars and offer from carsInCart?

Thus, what I want is something like this :

const result = [
   {chassis: 000000000, offer: 488},
   {chassis: 111111111, offer: 786}
]

Upvotes: 0

Views: 62

Answers (4)

Jake Whelan
Jake Whelan

Reputation: 64

Using for loops:

const allCars = [
   {id: 1, listID: 1, name: "Car 1", chassis: "000000000"},
   {id: 2, listID: 2, name: "Car 2", chassis: "111111111"},
   {id: 3, listID: 1, name: "Car 3", chassis: "222222222"},
   {id: 4, listID: 1, name: "Car 4", chassis: "333333333"}
];

const carsInCart = [
   {carID: 1, listID: 1, offer: 488},
   {carID: 2, listID: 2, offer: 786},
]

var result = []; //result goes here

//for each car
for(var i = 0; i < allCars.length; i++) {

    var car = allCars[i]; //current car

    //for each item in cart
    for(var k = 0; k < carsInCart.length; k++) {

        var cartItem = carsInCart[k]; //current cart item

        //if the current cart ID matches the current cart item ID
        if(car.id == cartItem.carID) {

            //its a match, add to result
            result.push({ chassis: car.chassis, offer: cartItem.offer });

        }

    }

}

Edit: .forEach() and .map() aren't supported by all browsers yet so they can't be used in web development.

Upvotes: 1

Lewis
Lewis

Reputation: 14866

Try this.

const result = carsInCart.map(carInCart => ({
  offer: carIncart.offer,
  chassis: allCars.find(car => car.id === carInCart.carID).chassis
}));

or even shorter with destructing assignment.

const result = carsInCart.map(({carID,offer}) => ({
  offer,
  chassis: allCars.find(({id}) => id === carID).chassis
}));

Upvotes: 1

hsz
hsz

Reputation: 152216

Just try with:

const result = carsInCart.map(function (car) {
  const aCar = allCars.find(function (c) {
    return c.id === car.ID;
  });
  return { chassis: aCar.chassis, offer: car.offer };
});

Or with functions shorthands:

const result = carsInCart.map(car => {
  chassis: allCars.find(c => c.id === car.ID).chassis,
  offer:   car.offer
});

Upvotes: 1

Nick Bull
Nick Bull

Reputation: 9866

const allCars = [
   {id: 1, listID: 1, name: "Car 1", chassis: "000000000"},
   {id: 2, listID: 2, name: "Car 2", chassis: "111111111"},
   {id: 3, listID: 1, name: "Car 3", chassis: "222222222"},
   {id: 4, listID: 1, name: "Car 4", chassis: "333333333"}
];

const carsInCart = [
  {carID: 1, listID: 1, offer: 488},
  {carID: 2, listID: 2, offer: 786},
]

var carsFilter = [];

carsInCart.forEach(function(car) {
  var matchingCar = jQuery.grep(array, function(item) {
    return item.id == car.carID;
  })[0];

  carsFilter.push({ 
    chassis: matchingCar.chassis, 
    offer: car.offer 
  });
});

// The result is the object `carsFilter`, if you didn't work that out!
console.log(carsFilter);

Upvotes: 0

Related Questions