I have an array of objects, some of these objects have the property "discount" with a value and some of them don't have the property at all. When I loop thru the array I would like to remove all the undefined elements because this is giving me an error. Here's some images to show you my issue:
var data = [
{
product: "Dusty Jumpsuit",
url: "",
image: "https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4d0de4b0edc898c43a14/1443712276073/kimem-dusty-jumpsuit_0358.jpg?format=2500w",
altDesc: "Blouse",
price: "$299.00",
discount: "$350.00"
},
{
product: "Jacky Trousers",
url: "",
image: "https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4d2fe4b0edc898c43b3c/1443712308758/kimem-jacky-bicolor-waist-trousers-navy-black_0374+%28Michelle+Liv%27s+conflicted+copy+2015-08-31%29.jpg?format=750w",
altDesc: "Pants",
price: "$228.00"
},
{
product: "Lisa Shirt",
url: "",
image: "https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4d67e4b094d8b81ba47a/1443712364244/kimem-lisa-oversized-shirt-navy_0363.jpg?format=750w",
altDesc: "Shirt",
price: "$253.00"
},
{
product: "Jane Skirt",
url: "",
image: "https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4d8fe4b0f1182e35da9a/1443712404868/kimem-long-pleated-skirt-black_0354.jpg?format=750w",
altDesc: "Shirt",
price: "$150.00",
discount: "$263.00"
},
];
function clothingView(item, index) {
return (`
<a href="${item.url}" class="shop-item">
<img src=${item.image} alt="${item.altDesc}" class="shop-item__img">
<div class="quickview">
<span class="quickview__icon">Quick View</span>
<span class="quickview__info">${item.product}
<br>
<span class="quickview__price">${item.price}<span class="quickview__price--discount">${item.discount}<span></span>
</span>
<span class="clothing-index">${index}</span>
</div>
</a>`)
};
var $productContainer = $('.products');
for (var i = 0; i < data.length; ++i) {
$productContainer.append( clothingView(data[i], i) )
}
* {
margin: 0;
padding: 0;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
font-size: 40%;
-webkit-font-smoothing: subpixel-antialiased;
}
body {
font-family: "Karla", sans-serif;
font-weight: 400;
line-height: 1.6;
color: #222;
}
.products {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.shop-item {
width: calc(100% / 5);
margin: 1rem;
position: relative;
display: flex;
justify-content: center;
}
.shop-item__img {
width: 100%;
height: 100%;
object-fit: cover;
}
.quickview {
width: 100%;
height: 100%;
position: absolute;
display: flex;
flex-direction: column;
justify-content: space-between;
align-content: center;
padding-bottom: 2rem;
color: #222;
letter-spacing: 0.15rem;
text-transform: uppercase;
opacity: 0;
transition: all ease-in-out 250ms;
}
.quickview:hover {
opacity: 1;
}
.quickview__icon {
font-size: 1rem;
background-color: rgba(204, 204, 204, 0.9);
padding: 0.7rem 1rem;
align-self: center;
}
.quickview__info {
font-size: 1.2rem;
align-self: center;
text-align: center;
}
.quickview__info--price {
font-size: 1rem;
color: rgba(61, 61, 61, 0.6);
}
.clothing-index {
display: none;
}
.quickview__price--discount {
margin-left: .5rem;
text-decoration: line-through;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="products all-items"></div>
I need to learn how to explain myself better, but I hope the images and the sample code is good enough to clarify the situation here. I tried adding {discount: ""} to all objects but I am not sure if that would be the best way to do this. Thanks in advance.

