js algorithm to find element position

Insert picture description here

Idea:
//In the array arr, find all the positions where the element with the same value as item appears.
// Idea: loop, and then judge if item and an item in the array ===, then put it into another array .
//Then output

function findAllOccurrences(arr, target) {
    
    
    var arr1=[];
    for(var i=0;i<arr.length;i++)
        {
    
    
            if(target===arr[i])
                {
    
    
                    arr1.push(i);
                }
        }
    return arr1;
}

Guess you like

Origin blog.csdn.net/qq_37805832/article/details/115280556