3

First array:

var products_cart = [1, 2, 2]; (these are the products_id's)

Second array:

var cart_details =
    [{
        product_id: 1,
        product_name: 'product love',
        category_name: 'love'
    },
    {
        product_id: 2,
        product_name: 'product birthday love',
        category_name: 'birthday'
    }]

I want to loop through the elements of the array products_cart and use the product_id from products_cart to render details from cart_details (loop through them I suppose), how is this possible?

EDIT:

I think some of you got me wrong, I basically want this to render something like this:

[{
    product_id: 1,
    product_name: 'product love',
    category_name: 'love'
},
{
    product_id: 2,
    product_name: 'product birthday love',
    category_name: 'birthday'
},
{
    product_id: 2,
    product_name: 'product birthday love',
    category_name: 'birthday'
}
]

My mistake, I wasn't clear enough.

4
  • 1
    What should the result look like? Commented Nov 12, 2017 at 22:53
  • there should be 3 results that use the details from cart_details Commented Nov 12, 2017 at 23:05
  • There shouldn't be duplicates in product_cart!? Commented Nov 12, 2017 at 23:11
  • Yes, there should! I want 2 to show twice, not once. Commented Nov 12, 2017 at 23:18

5 Answers 5

1

Assuming that the values of the products_cart are product id values, then you can try the following:

var products_cart = [1, 2, 2];

var cart_details = [{
    product_id: 1,
    product_name: 'product love',
    category_name: 'love'
  },
  {
    product_id: 2,
    product_name: 'product birthday love',
    category_name: 'birthday'
  }
]

for (var i=0, max=products_cart.length; i < max; i++) {
  for (var j=0, maxi = cart_details.length; j < maxi; j++) {
  
  if ( products_cart[i] == cart_details[j].product_id) {
       console.log( "product id: " + cart_details[j].product_id);
       console.log("product name: " + cart_details[j].product_name); 
       console.log("category name: " + cart_details[j].category_name,"\n\n");
      }
    
    }
  }

Sign up to request clarification or add additional context in comments.

3 Comments

I want it to return 3 results, reread the question again I also edited it.
@Cedric Okay, I fixed it -- hope this works for you. It is a pure JavaScript solution.
@Cedric You're welcome; I'm glad you like my answer.
1

You could use find() while looping through your ids array:

let products = [];

products_cart.forEach(id => {
    let product = cart_details.find(detail => detail.product_id === id);

    if (product) {
       products.push(product);
    }
});

5 Comments

@CedricHadjian it definitely does. but i used es6 features, might that be your problem? where do you run your code?
EJS (express.js app)
when I logged products, it returned an empty array
@CedricHadjian Take a look, i used your example arrays from above and the output is the one you are expecting: jsfiddle.net/h1qtfjbp/2 So maybe you have some error in your implementation.
slevy1's answer worked, check it out. Wanted something like that.
0

var products_cart = [1, 2, 2];
var cart_details = [{
product_id: 1,
product_name: 'product love',
category_name: 'love'
},
{
product_id: 2,
product_name: 'product birthday love',
category_name: 'birthday'
}];

products_cart.forEach(function(id){
  cart_details.forEach(function(detail){
    if(detail.product_id === id){
      console.log(detail);
    }
  });
});

1 Comment

I think you got me wrong, I edited the post, check it out.
0

What I would recommend doing is looping over the products_cart with .forEach(), then checking the keys of cart_details with Object.keys(), and looping over them as well. Then you can simply check whether the two match.

You then have a collection of all objects that have a product_id that is in your products_cart, and are free to do with them as you will.

The following example simply logs the objects back to you:

var products_cart = [1, 2, 2];

var cart_details = [{
    product_id: 1,
    product_name: 'product love',
    category_name: 'love'
  },
  {
    product_id: 2,
    product_name: 'product birthday love',
    category_name: 'birthday'
  },
  {
    product_id: 3,
    product_name: 'NOT IN ARRAY products_cart',
    category_name: 'SHOULD NOT BE SHOWN'
  }
]


products_cart.forEach(function(product) {
  Object.keys(cart_details).forEach(function(key) {
    if (product === cart_details[key].product_id) {
      console.log(cart_details[key]);
    }
  });
});

I've also included a third product to demonstrate that only products in products_cart will be shown in the conditional.

Hope this helps! :)

7 Comments

I think you got me wrong, I edited the post, check it out.
@CedricHadjian - I've updated my answer to log product 2 twice :)
product_id is not defined :(
@CedricHadjian -- It is working in the above snippet. Ensure you have copied it exactly, and then build up from there to find the cause of the problem.
The thing is I'm using express.js, therefore I needed to remove Object.keys(), if I don't remove it it's not returning anything, if I remove it it's returning error (product_id is not defined). I thought this could be solved through simple for loop and there wasn't a need to note that it's an express.js application. :/
|
0

Do you need list? I think using json instead of list.

var products_cart = [1, 2, 2];

var cart_details =[
{
    product_id: 1,
    product_name: 'product love',
    category_name: 'love'
},
{
    product_id: 2,
    product_name: 'product birthday love',
    category_name: 'birthday'
}];

var new_cart_details = {};

// changed list to josn
cart_details.map( item => {
    new_cart_details[item.product_id] = {
        product_name: item.product_name,
        category_name: item.category_name
    }
});

products_cart.map( item => {
    console.log(new_cart_details[item])
});

3 Comments

Yes, I do need a list, I need to loop through the result to show it in the view. This returned 3 separate objects which was what I was looking for but I need them inside an array.
hmm. then Do you my propose is not solved. The method I suggested is also not a problem with turning the loop around
This worked with me but it's a lot longer, @slevy1's solution was simple and straight forward. Thank you for your time!

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.