Any way to reduce the no of lines in js code

I want to get the text with ‘In,or May be’ from list of div records with In, Out and May be displaying and if text match with ‘In / May be’, do some other action;

<div id ="ply_grid" class="row">		
			  <div class="col-md-2"><input type="checkbox" name="" value=""></div>
			  <div class="col-md-3"><img id="mypic" class="img-responsive" src="images/foo36.jpg"></div>
			  <div class="col-md-2"><label for="pname" id="names1" class="hd">In </label></div>
			  <div class="col-md-3">25% of </div>
			  <div class="col-md-2">25% of </div>		
		</div>
		<div id ="ply_grid" class="row">		
			  <div class="col-md-2"><input type="checkbox" name="" value=""></div>
			  <div class="col-md-3"><img id="mypic" class="img-responsive" src="images/foo37.png"></div>
			  <div class="col-md-2"><label for="pname" id="names1" class="hd">Out </label></div>
			  <div class="col-md-3">26% of </div>
			  		
		</div>

Following javascript is working fine for me, but could someone advise if there is any other better way to reduces the no of lines in code

var divLabels = new Array();
var finalArr1 = new Array();

function displayLabel(){
	divLabels = document.getElementsByTagName('label');
	var searchIn = "In";
	var searchMaybe = "May be";	
	
	for (i=0; i < divLabels.length; i++){
		if (divLabels[i].innerText == searchIn || divLabels[i].innerText == searchMaybe ) {
			finalArr1.push(divLabels[i]);
			for(j=0; j < finalArr1.length; j++){
			alert(finalArr1[j].innerText);
				// based on the above result do some action here 
			}
			
		}

	}

}

Is this condensed enough?

function displayLabel(){
    divLabels = document.getElementsByTagName('label');
    var searchTerms = ["In", "May be"]; 
    Array.from(divLabels).map(div => div.innerText)
        .filter(text => searchTerms.some(term => text === term))
        .forEach(function (text) {
            console.log(text);
        });
}
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.