I am adding elements to an array like this..
myarray = [];
myarray.push($('#name1').text());
myarray.push($('#name2').text());
myarray.push($('#name3').text());
myarray.push($('#name4').text());
This is working correctly, but i am trying to only push to the array if the element exists and has content.
I know I can go through the array afterwards and strip out any empty values but is there a way I can do it first to save having that extra step?
>Solution :
If it’s about the codes length there are shorter ways to write this.
const myarray = ['#name1','#name2','#name3','#name4']
.map(n => $(n).text()).filter(t=>t);