[js] The usage of findIndex() and a small note about stepping on the pit~

effect

Find the position of the first element of the array that meets the criteria.

need

In a two-dimensional array, traverse the sub-array, if learnStatus == 0 in the sub-array, then tabIndex is the subscript of the parent array, otherwise tabIndex = 0

accomplish

	var i;
	var idx;
	i = this.ganyuPlanList.findIndex((item, index) => {
    
    
		idx = item.tbAppMeddleListOfLearnpackList.findIndex(item2 => {
    
    
		return item2.learnStatus == 0;
		});
		return idx > -1
	});
	if(i > -1){
    
    
		this.tabIndex = i;
	}else{
    
    
		this.tabIndex = 0
	}

Step on the pit

The actual return needs to meet the conditions, that is, it is actually return true , if it is false, it will continue to traverse, if the condition is not met after traversing, it will return -1
and my mistake lies in the misunderstanding of return, understanding return The thing is that I need to return the subscript, or as long as I return, the traversal will be blocked. In fact, as long as I don’t return true, I will continue to traverse

For multi-dimensional array traversal, it is especially easy to accidentally change the concept~

Mark~
insert image description here

Guess you like

Origin blog.csdn.net/qq_45481971/article/details/131976797