Array.find 方法 return 是否是给定数组中匹配元素的副本或引用?
Does Array.find method return a copy or a reference of the matched element form given array?
Array.find 方法 returns 对找到的值的某些特定副本或数组中的引用赋值是什么。我的意思是 returns 给定数组中匹配元素的值或引用。
来自 MDN(强调他们的):
The find() method returns the value of the first element in the
provided array that satisfies the provided testing function.
是否 returns 值的副本或对值的引用将遵循正常的 JavaScript 行为,即如果它是原始值,它将是一个副本,如果它是一个复杂值,它将是一个引用类型。
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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
const obj = {}
console.log(obj.find)
const arr = ['a', 'b', 'c']
console.log(arr.find(e => e === 'a'))
console.log(arr.find(e => e ==='c'))
Returns 值
find() 方法 returns 数组中第一个通过测试的元素的值(作为函数提供)。
find() 方法对数组中存在的每个元素执行一次函数:
如果找到函数 returns 为真值的数组元素,则 find() returns 该数组元素的值(并且不检查剩余值)
否则,它 returns undefined
这是一个棘手的问题。
从技术上讲,find
总是 returns 一个值,但如果您要查找的项目是一个对象,则该值可能是一个参考。尽管如此,它仍然是一个值。
这与此处发生的情况类似:
let a = { some: "object" };
let b = a;
您正在将变量 a
的 值 复制到 b
。恰好该值是对象 { some: "object" }
.
的 reference
Array.find 方法 returns 对找到的值的某些特定副本或数组中的引用赋值是什么。我的意思是 returns 给定数组中匹配元素的值或引用。
来自 MDN(强调他们的):
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
是否 returns 值的副本或对值的引用将遵循正常的 JavaScript 行为,即如果它是原始值,它将是一个副本,如果它是一个复杂值,它将是一个引用类型。
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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
const obj = {}
console.log(obj.find)
const arr = ['a', 'b', 'c']
console.log(arr.find(e => e === 'a'))
console.log(arr.find(e => e ==='c'))
Returns 值
find() 方法 returns 数组中第一个通过测试的元素的值(作为函数提供)。
find() 方法对数组中存在的每个元素执行一次函数:
如果找到函数 returns 为真值的数组元素,则 find() returns 该数组元素的值(并且不检查剩余值) 否则,它 returns undefined
这是一个棘手的问题。
从技术上讲,find
总是 returns 一个值,但如果您要查找的项目是一个对象,则该值可能是一个参考。尽管如此,它仍然是一个值。
这与此处发生的情况类似:
let a = { some: "object" };
let b = a;
您正在将变量 a
的 值 复制到 b
。恰好该值是对象 { some: "object" }
.