0

So this is kind of hard to explain, but basically trying to make a more efficient way of doing the below code...

products.forEach(function(item, index){
    if(item.sale == false ) {
        nonSaleItems += createEl(item);
    } else {
        saleItems += createEl(item);
    }
});

items.innerHTML += nonSaleItems;
items.innerHTML += saleItems;

products is an array of objects where one of the keys is "sale" and is "false" or "true" for "sale". The idea behind this is to post all the none sale items first, then the sale items goes second - hence the innerHTML of nonSaleItems first, THEN saleItems gets added second.

This code works perfectly, but I feel like there has to be a more efficient / less verbose way to do it instead of two variables of nonSaleItems and saleItems and then two innterHTMLs.

Just curious if anyone had better ideas on how to simplify this or have it better?

Alfred
  • 23
  • 3
  • 1
    Might want to check out codereview.stackexchange. Stackoverflow is generally more focused on fixing broken code than evaluating working code. – CollinD May 18 '22 at 15:30

1 Answers1

0

You're already doing one loop through all the products, which is a) necessary, and b) about as efficient as it gets.

The best you can do is make your code less verbose. In this case I'm using a ternary to decide which whether to use saleItems or nonSaleItems as the lvalue for += createEl(...):

products.forEach(function(item, index){
    ( item.sale ? saleItems : nonSaleItems ) += createEl(item);
});

items.innerHTML += nonSaleItems;
items.innerHTML += saleItems;

EDIT:

The above code won't work because the ternary evaluates to an rvalue, not an lvalue. See Javascript Ternary Operator lvalue.

However, you can use the ternary to evaluate to an object, which can be dereferenced (see https://stackoverflow.com/a/18668615/378779):

let saleItems = { html: '' }, nonSaleItems = { html: '' }
products.forEach(function(item, index){
    ( item.sale ? saleItems : nonSaleItems ).html += createEl(item);
});

items.innerHTML += nonSaleItems.html;
items.innerHTML += saleItems.html;

Or, with only one object, but using a ternary to determine the key:

let html = { sale: '', nonSale: '' };
products.forEach(function(item, index){
    html[(item.sale ? 'sale' : 'nonSale')] += createEl(item);
});

items.innerHTML += html.nonSale;
items.innerHTML += html.sale;
kmoser
  • 8,780
  • 3
  • 24
  • 40
  • Is it possible to use a ternary operator on the left part of the operation? (AFAIK is invalid) – Pipe May 18 '22 at 15:33
  • Ah okay thank you! Yeah was just trying to see if maybe something I didn't know or have learned yet would make it better just seemed inefficient haha. – Alfred May 18 '22 at 17:57
  • @Pipe You are right. See my updated answer. – kmoser May 19 '22 at 04:45