0

I have a loop that looks like this:

var selectionList = document.querySelectorAll(".product");

selectionList.forEach(selectionItem => {
    var selectionItemTitle = selectionItem.querySelector(".product-title");
}

Inside of my loop I want to ad a condition that says "If this title is not equal to NONE, add this to an array with all the others. See below my note in the code:

selectionList.forEach(selectionItem => {
    var selectionItemTitle = selectionItem.querySelector(".product-title");
    if(selectionItemTitle.textContent != 'None'){
        
        // this is where I am stuck (I don't know what to add inside of my condition)

    }
}

Basically what I want to do is, all the "selectionItemTitle" that passes the conditon needs to be added to an array so I can use that array some place else.

Is this even possible?

Quick note: The reason why I am using a loop, is because I have lots of other code in it (the above is just an example). So I really want to stick to the loop.

2
  • You're stuck on adding items to an array? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 12, 2021 at 10:16
  • I don't think I understand your question. You can create your array before the forEach and then use push if the if condition is true. Commented Feb 12, 2021 at 10:16

2 Answers 2

2

If I understand the question correctly, you can create your array and then add to it with push, see comments:

const theArray = []; // *** Create the array
selectionList.forEach(selectionItem => {
    var selectionItemTitle = selectionItem.querySelector(".product-title");
    if(selectionItemTitle.textContent != 'None'){
        
        theArray.push(selectionItemTitle); // *** Add to it

    }
});

But that's if you want to keep the forEach. You could use filter instead, still doing the other work in the filter callback:

const theArray = [...selectionList].filter(selectionItem => {
    var selectionItemTitle = selectionItem.querySelector(".product-title");
    // At the end, decide whether this entry is kept or discarded
    // (true for keep, false for discard)
    return selectionItemTitle.textContent != 'None';
});

Note the spread so that we turn the NodeList from querySeletorAll into a true array (so it has filter). You can also just use filter directly on the NodeList:

const theArray = Array.prototype.filter.call(selectionList, selectionItem => {
    var selectionItemTitle = selectionItem.querySelector(".product-title");
    // At the end, decide whether this entry is kept or discarded
    // (true for keep, false for discard)
    return selectionItemTitle.textContent != 'None';
});
Sign up to request clarification or add additional context in comments.

Comments

0

Declare a variable and assign it an empty array. While looping, if the condition is met, push into the empty array.

Comments

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.