0

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}
]
1
  • Note: you appear to be using const where var, or let would be more appropriate. Using them as you have implies that you might have some confusion as to where to use each of these statements Commented Aug 4, 2016 at 9:36

4 Answers 4

1

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

One word : Brilliant! :)
1

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
}));

Comments

1

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.

Comments

0
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);

3 Comments

no idea why the downvote. Note the result was to be result, not carsFilter, even if carsFilter is more descriptive.
OP didn't mention jQuery. I don't think you should use it.
Thanks for the answer, but I do not want to use jquery.

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.