Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  161] [ 6]  / answers: 1 / hits: 6887  / 4 Years ago, sun, june 21, 2020, 12:00:00

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.


More From » arrays

 Answers
10

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




[#3419] Thursday, June 18, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
josefn questions
Sat, Mar 28, 20, 00:00, 4 Years ago
Fri, Feb 21, 20, 00:00, 4 Years ago
Wed, Oct 30, 19, 00:00, 5 Years ago
;