gpt4 book ai didi

javascript - 是否可以创建无限维 JavaScript 数组?

转载 作者:行者123 更新时间:2023-11-28 20:42:36 25 4
gpt4 key购买 nike

我正在尝试在 JavaScript 中创建(或模拟)一个无限维数组。本质上,这将是一个将对象与整数列表(可以是任意长度)相关联的数据结构。有没有一种有效的方法来存储此数据结构中的每个元素?

function addElement(theObject, coordinates){
//object is the object, and coordinates is the list of coordinates (any number of coordinates will be accepted, since it's infinite-dimensional)
}

function getObject(coordinates){
//get the object that was previously assigned to this list of coordinates
}
addElement("Hello World", [0, 0, 3, 5]);
console.log(getObject([0, 0, 3, 5])); //this would print "Hello World".

最佳答案

除非有任何原因不能,否则我只会使用坐标作为索引,并将内容存储在那里:

var coordinates = [];
var testCoord = [0,0,3,5];
coordinates[testCoord] = "Hello World";
console.log(coordinates[testCoord]);

关于javascript - 是否可以创建无限维 JavaScript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107187/

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