Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing argument to array.prototype.findIndex(callback[, thisArg])

I am having trouble passing an argument to the javascript array.prototype.findIndex(callback[, thisArg]) API. The javascript documentation specifies that an argument can be passed but I have not had luck. This is the API reference Array.prototype.findIndex()

I see this syntax in API:

arr.findIndex(callback[, thisArg])

This is my current implementation but I don't like the way it is implemented with a globally created variable it is not easibly tested code.

var vm = this; //angular context
//List of objects
vm.someList = [{storeId: "1", name:"Carmel", quantity:"25", orderNumber:"12345"},
		       {storeId: "2", name:"Chocolate", quantity:"25", orderNumber:"23456"},
		       {storeId: "3", name:"Vanilla", quantity:"25", orderNumber:"34567"},
		       {storeId: "4", name:"Cinnamon", quantity:"25", orderNumber:"45678"}];
		
//List of items marked with priority
vm.priorityList = [{storeId: "3", name:"Vanilla", quantity:"25", orderNumber:"34567", priority: "true"}];
		
//For loop finds index of criteria to perform operation on someList[i] element
for(var i =0; i < vm.priorityList.length; i++){
	//itemToFind created in global scope.
        vm.itemToFind = vm.priorityList[i];
	var index = vm.someList.findIndex(findPriorityIndex);
	//Don't worry about the operation being performed.
    console.log(index);
}

//callback fn used
function findPriorityIndex(element, index, array){
     return (element.storeId === vm.itemToFind.storeId
        && element.orderNumber ===vm.itemToFind.orderNumber);
}

I have tried to pass the argument to be used as thisArg in the callback with the following syntax but all resulted in failed attempts. Also I do acknowledge the API mentions the argument will be referenced as this inside the callback, and I have made such adjustment in the callback function.

vm.someList.findIndex(findPriorityIndex[, vm.priorityList[i]]);
vm.someList.findIndex(findPriorityIndex(vm.priorityList[i]));
vm.someList.findIndex([findPriorityIndex, vm.priorityList[i]]);

If anyone can provide any information as to how the argument should be passed to the callback function I will greatly appreciate it.

like image 318
TerNovi Avatar asked Aug 12 '26 20:08

TerNovi


2 Answers

Basically you set with thisArg of findIndex, this in the callback.

for (var i = 0; i < vm.priorityList.length; i++) {
    index = vm.someList.findIndex(findPriorityIndex, vm.priorityList[i]);´
    //                                   set thisArg ^^^^^^^^^^^^^^^^^^
    console.log(index);
}

// callback fn used
function findPriorityIndex(element, index, array) {
    return element.storeId === this.storeId && element.orderNumber === this.orderNumber;
    //        access this here ^^^^                                and ^^^^
}
like image 158
Nina Scholz Avatar answered Aug 14 '26 11:08

Nina Scholz


In simple words, use this to access the argument as explained below

const fruits = [{id:1,label:"apple"},{id:2,label: "banana"}, {id:3,label:"cantaloupe"}
            ];
function checkIndex(element) {

    return element.label == this;
}

console.log(fruits.findIndex(checkIndex,'banana'));
like image 28
Feras Avatar answered Aug 14 '26 10:08

Feras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!