What does Array.find method returns value some specifical copy of a found value or the reference from the array. I mean what it returns value or reference of the matched element from the given array.
The find() method returns the first element in the provided array that satisfies the provided testing function.
Array/object values are copied by reference instead of by value.
JavaScript pop() Method: The pop() method is a built-in method that removes the last element from an array and returns that element. This method reduces the length of the array by 1. Syntax: array.
To find the first array element that matches a condition:Use the Array. find() method to iterate over the array. Check if each value matches the condition. The find method returns the first array element that satisfies the condition.
From MDN (emphasis theirs):
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
Whether it returns a copy of or a reference to the value will follow normal JavaScript behaviour, i.e. it'll be a copy if it's a primitive, or a reference if it's a complex type.
let foo = ['a', {bar: 1}];
let a = foo.find(val => val === 'a');
a = 'b';
console.log(foo[0]); //still "a"
let obj = foo.find(val => val.bar);
obj.bar = 2;
console.log(foo[1].bar); //2 - reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With