I am going to try to explain this as simple as possible. I am trying to filter an array (data) and return or push the filtered data to a new filtered array (filteredData). This is an example of how I am doing it:
let filteredData = [];
console.log(filteredData);
$('.categories__link').click(function() {
const category = this.id;
let filteringData;
if (category === 'bottoms') {
filteringData = data.filter(el => el.category === 'bottoms');
} else if (category === 'tops') {
filteringData = data.filter(el => el.category === 'tops');
} else if (category === 'outwear') {
filteringData = data.filter(el => el.category === 'outwear');
}
console.log(filteringData);
Array.prototype.push.apply(filteredData, filteringData);
});
When I test it in the console, it doesn’t look as expected:
It says it’s an array and it shows the filtered data, but it doesn’t look like an array, is this a bug in the console or am I doing something wrong? I ask this because it’s not showing me the number of objects in the array like the one below “Live reload enabled.” text, plus if I open it (in the console) to see if there’s any data and I keep clicking different sections, it won’t add any more data.
This is the html structure for better understanding:
<section class="products-container container">
<nav class="categories">
<span class="categories__link" id="accessories">Accessories</span>
<span class="categories__link" id="bottoms">Bottoms</span>
<span class="categories__link" id="dresses">Dresses + Jumpsuits</span>
<span class="categories__link" id="outwear">Outerwear</span>
<span class="categories__link" id="tops">Tops</span>
<span class="categories__link" id="sale">— Sale</span>
</nav>
<div class="products all-items"></div>
<div class="products accessories"></div>
<div class="products bottoms"></div>
<div class="products dresses"></div>
<div class="products outwear"></div>
<div class="products tops"></div>
<div class="products sale"></div>
</section>
The data:
const data = [
{
product: "Haori Jacket",
url: "haorijacket.html",
image: "img/haori-jacket.jpg",
altDesc: "Jacket",
price: "$210.00",
category: "outwear"
},
{
product: "Swing Dress",
url: "swingdress.html",
image: "img/swing-dress.jpg",
altDesc: "Dress",
price: "$218.00",
category: "dresses"
},
{
product: "Lounge Jumpsuit",
url: "",
image: "img/lounge-jumpsuit.jpg",
altDesc: "Jumpsuit",
price: "$298.00",
category: "dresses"
},
{
product: "Linen Top",
url: "",
image: "img/linen-top.jpg",
altDesc: "Blouse",
price: "$125.00",
category: "tops"
},
{
product: "Lounge Tunic",
url: "",
image: "img/lounge-tunic.jpg",
altDesc: "Dress",
price: "$258.00",
category: "dresses"
},
{
product: "Sonia Dress",
url: "",
image: "img/sonia-dress.jpg",
altDesc: "Dress",
price: "$268.00",
category: "dresses"
},
{
product: "White Studio Top",
url: "",
image: "img/white-studio-top.jpg",
altDesc: "Blouse",
price: "$158.00",
category: "tops"
},
{
product: "Swing Top",
url: "",
image: "img/swing-top.jpg",
altDesc: "Blouse",
price: "$128.00",
category: "tops"
},
{
product: "Terra Cota Studio Top",
url: "",
image: "img/terracota-studio-top.jpg",
altDesc: "Blouse",
price: "$158.00",
category: "tops"
},
{
product: "Wide Pant",
url: "",
image: "img/wide-pant.jpg",
altDesc: "Pants",
price: "$190.00",
category: "bottoms"
},
{
product: "Charcoal Long Shorts",
url: "",
image: "img/charcoal-long-shorts.jpg",
altDesc: "Pants",
price: "$200.00",
category: "bottoms"
},
{
product: "Linen Long Shorts",
url: "",
image: "img/linen-long-shorts.jpg",
altDesc: "Pants",
price: "$200.00",
category: "bottoms"
},
{
product: "Diana Jacket",
url: "",
image: "img/diana-jacket.jpg",
altDesc: "Jacket",
price: "$368.00",
category: "outwear"
},
{
product: "Poplin Chef Shirt",
url: "",
image: "img/poplin-chef-shirt.jpg",
altDesc: "Shirt",
price: "$216.00",
category: "tops"
},
{
product: "Poplin Dress",
url: "",
image: "img/poplin-dress.jpg",
altDesc: "Dress",
price: "$231.00",
category: "dresses"
},
{
product: "Layer Dress",
url: "",
image: "img/layer-dress.jpg",
altDesc: "Dress",
price: "$309.00",
category: "dresses"
},
{
product: "Poplin V Top",
url: "",
image: "img/poplin-vtop.jpg",
altDesc: "Blouse",
price: "$143.00",
category: "tops"
},
{
product: "3 Button Tunic",
url: "",
image: "img/3button-tunic.jpg",
altDesc: "Blouse",
price: "$250.00",
category: "dresses"
},
{
product: "Charcoal Tunic",
url: "",
image: "img/charcoal-tunic.jpg",
altDesc: "Blouse",
price: "$250.00",
category: "dresses"
},
{
product: "Dusty Jumpsuit",
url: "",
image: "img/dusty-jumpsuit.jpg",
altDesc: "Jumpsuit",
price: "$299.00",
sale: "$399.00",
category: "dresses"
},
{
product: "Jacky Trousers",
url: "",
image: "img/jacky-trousers.jpg",
altDesc: "Pants",
price: "$228.00",
category: "bottoms"
},
{
product: "Lisa Shirt",
url: "",
image: "img/lisa-shirt.jpg",
altDesc: "Shirt",
price: "$253.00",
category: "tops"
},
{
product: "Jane Skirt",
url: "",
image: "img/jane-skirt.jpg",
altDesc: "Shirt",
price: "$150.00",
sale: "$263.00",
category: "bottoms"
},
{
product: "Romy Top",
url: "",
image: "img/romy-top.jpg",
altDesc: "Blouse",
price: "$199.00",
sale: "$300.00",
category: "tops"
},
{
product: "Romy Trousers",
url: "",
image: "img/romy-trousers.jpg",
altDesc: "Pants",
price: "$250.00",
sale: "$310.00",
category: "bottoms"
},
{
product: "Twiggy Dress",
url: "",
image: "img/twiggy-dress.jpg",
altDesc: "Dress",
price: "$200.00",
category: "dresses"
},
{
product: "Sonia Skirt",
url: "",
image: "img/sonia-skirt.jpg",
altDesc: "Skirt",
price: "$228.00",
category: "bottoms"
},
{
product: "Duo-tone Backpack",
url: "",
image: "img/duotone-backpack.jpeg",
altDesc: "Backpack",
price: "$420.00",
category: "accessories"
},
{
product: "Black Backpack",
url: "",
image: "img/black-backpack.jpeg",
altDesc: "Backpack",
price: "$420.00",
category: "accessories"
},
{
product: "Bucket Tote",
url: "",
image: "img/bucket-tote.jpeg",
altDesc: "Backpack",
price: "$500.00",
category: "accessories"
},
{
product: "Black Crossbody",
url: "",
image: "img/black-crossbody.jpeg",
altDesc: "Backpack",
price: "$175.00",
sale: "$250.00",
category: "accessories"
},
{
product: "Tan Crossbody",
url: "",
image: "img/tan-crossbody.jpeg",
altDesc: "Backpack",
price: "$175.00",
sale: "$250.00",
category: "accessories"
},
{
product: "Black Tote",
url: "",
image: "img/black-tote.jpeg",
altDesc: "Backpack",
price: "$350.00",
sale: "$475.00",
category: "accessories"
},
{
product: "Tan Tote",
url: "",
image: "img/tan-tote.jpeg",
altDesc: "Backpack",
price: "$350.00",
sale: "$475.00",
category: "accessories",
},
{
product: "Sunglasses Nº 1",
url: "",
image: "img/sunglasses-n1.jpeg",
altDesc: "Sunglasses",
price: "$125.00",
category: "accessories"
},
{
product: "Sunglasses Nº 2",
url: "",
image: "img/sunglasses-n2.jpeg",
altDesc: "Sunglasses",
price: "$125.00",
category: "accessories"
},
{
product: "Sunglasses Nº 3",
url: "",
image: "img/sunglasses-n3.jpeg",
altDesc: "Sunglasses",
price: "$125.00",
category: "accessories"
},
{
product: "Sunglasses Nº 4",
url: "",
image: "img/sunglasses-n4.jpeg",
altDesc: "Sunglasses",
price: "$125.00",
category: "accessories"
}
];