API Testing ‘A Beginners View’: JavaScript - Array.prototype.find() and Array.prototype.findIndex()
Array.prototype.find()
The find() method returns the value of the first value of the array to be tested which passes the condition specified. It checks all the values in the array and whichever value satisfies the condition first would be picked and displayed in the output. If the value is not found it returns undefined.
Let’s check out some examples to understand it better;
Example1: Using Arrow Function
Response Body:
{
"args":{
},
"data":{
"FirstName":[
{
"value":"{{RandomFirstName}}"
}
],
"Specialty":[
{
"name":"NEUROLOGY",
"experience":"2"
},
{
"name":"Dermatology",
"experience":"2"
},
{
"name":"Ortho",
"experience":"2"
}
]
},
"files":{
},
"form":{
},
"headers":{
"x-forwarded-proto":"https",
"x-forwarded-port":"443",
"host":"postman-echo.com",
"x-amzn-trace-id":"Root=1-5fd86d42-7d196eb86048629c2b3ea2ca",
"content-length":"288",
"content-type":"application/json",
"user-agent":"PostmanRuntime/7.26.8",
"accept":"*/*",
"cache-control":"no-cache",
"postman-token":"065034d3-4e05-4d1b-872d-382f2579524d",
"accept-encoding":"gzip, deflate, br",
"cookie":"sails.sid=s%3A6n5cRYOmPZd0CgxhkfCCHO6rFlTzSFfw.9ZTgkbZcBTzwWSiBLXKE%2BktjLAb6IdSuIMFNxLxnK9Y"
},
"json":{
"FirstName":[
{
"value":"{{RandomFirstName}}"
}
],
"Specialty":[
{
"name":"NEUROLOGY",
"experience":"2"
},
{
"name":"Dermatology",
"experience":"2"
},
{
"name":"Ortho",
"experience":"2"
}
]
},
"url":"https://postman-echo.com/post"
}
Test Script:
//Using function how to find an object in array by 'find' method
var resp = JSON.parse(responseBody);
function find_result1(value) {
return value.name === 'Dermatology';
}
console.log(array_value.find(find_result1));
Console Output:
Example 2: Using function how to find an object in array by ‘find’ method
Test Script:
var resp = JSON.parse(responseBody);
console.log(resp);
const array_value = resp.data.Specialty;
console.log(array_value);
//Using arrow function how to find an object in array by 'find' method
const result = array_value.find( ({ name }) => name === 'NEUROLOGY' );
console.log(result);
Console Output:
The explanation for the above examples:
The above example is based on an array that has three values to it as below:
I have used the ‘find’ method in two different ways to identify the object that satisfies the conditions:
- name === ‘NEUROLOGY’
- name === ‘Dermatology’
After execution, the output returns the value which satisfied the condition.
Array.prototype.findIndex()
In continuation of the above feature, if we need to know the index of the element that we found, this can be done by the findIndex() method. It returns the index of the first element in the array that satisfies the condition we provide. If no element passes the test condition, it returns -1.
Example: In continuation to the above snippet when we add the below code, index will be returned
//How to find the index of the result
//Using function how to find an object in array by 'find' method
function find_result1(value) {
return value.name === 'Dermatology';
}
console.log(array_value.find(find_result1));
console.log(array_value.findIndex(find_result1)); //this is using function
Console Output:
Hope this was useful
Happy Learning