最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Looping through the array inside another array - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Aug 26, 2024 at 10:05 Boshra Jaber 1,1556 gold badges17 silver badges33 bronze badges asked Nov 12, 2017 at 22:48 Cedric HadjianCedric Hadjian 91413 silver badges32 bronze badges 4
  • 1 What should the result look like? – user3589620 Commented Nov 12, 2017 at 22:53
  • there should be 3 results that use the details from cart_details – Cedric Hadjian Commented Nov 12, 2017 at 23:05
  • There shouldn't be duplicates in product_cart!? – user3589620 Commented Nov 12, 2017 at 23:11
  • Yes, there should! I want 2 to show twice, not once. – Cedric Hadjian Commented Nov 12, 2017 at 23:18
Add a ment  | 

5 Answers 5

Reset to default 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");
      }
    
    }
  }

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

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

What I would remend 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! :)

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])
});
发布评论

评论列表(0)

  1. 暂无评论