gpt4 book ai didi

Javascript 返回对具有原始值的数组索引的引用

转载 作者:行者123 更新时间:2023-12-02 23:04:07 33 4
gpt4 key购买 nike

我想返回对索引 x 处数组内容的引用,以便之后能够使用返回的索引更改数组内容。这是我的意思的一个例子:

let testArr = [1,2,3]
const someFunct = arr => {
...
return {reference to arr[0], aka 1}
}
someFunct(testArr) = 0;

//should log [0,2,3]
console.log(testArr);
在这种情况下,

someFunct(testArr) 的行为应类似于 arr[0]
数组的内容可以是任何内容。

最佳答案

我认为您想要实现的确切实现在 JavaScript 中是不可能的。

https://medium.com/@naveenkarippai/learning-how-references-work-in-javascript-a066a4e15600

类似的东西:

const testArr = [1,2,3]
const changeArray = (array, index, newValue) => {
array[index] = newValue
return array
}

changeArray(testArr, 0, 0) // evaluates to [0,2,3]

关于Javascript 返回对具有原始值的数组索引的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57662819/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com