JavaScript在多维数组中设置值,其中维度不是预定义的
假设我有一个空的1D数组: var数据=[] 现在,我想给数据[1][1][3]增加值1 为此,我需要将数据数组扩展到:JavaScript在多维数组中设置值,其中维度不是预定义的,javascript,multidimensional-array,resize,extend,Javascript,Multidimensional Array,Resize,Extend,假设我有一个空的1D数组: var数据=[] 现在,我想给数据[1][1][3]增加值1 为此,我需要将数据数组扩展到: data = [ [], [], [[],[],[1]] ] 是的,非常确定我必须编写一个函数,并将维度值作为1D数组[1,1,3]传递,然后检查1。如果有第二、第三维度,如果“否”,则创建它们和2。如果其大小大于或等于 对于这个例子,函数是 function setData(value_, index_){ if(data[index_[0]] == un
data = [
[],
[],
[[],[],[1]]
]
是的,非常确定我必须编写一个函数,并将维度值作为1D数组[1,1,3]传递,然后检查1。如果有第二、第三维度,如果“否”,则创建它们和2。如果其大小大于或等于
对于这个例子,函数是
function setData(value_, index_){
if(data[index_[0]] == undefined){
data = Array(index_[0] + 1).fill([]);
data[index_[0]] = Array(index_[1] + 1).fill(1);
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}else{
if(data[index_[0]][index_[1]] == undefined){
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}
}
}
它笨拙而直接向前。我怎样才能把它变成一个普遍的东西?
对于任何尺寸
var数据=[];
setData(假[1,1,3]);
函数setData(值、索引){
if(数据[索引[0]]==未定义){
数据=数组(索引[0]+1)。填充([]);
数据[索引[0]]=数组(索引[1]+1)。填充(1);
数据[索引[0]][索引[1]]=数组(索引[2]+1).填充([]);
数据[索引[0]][索引[1]][索引[2]]=值;
}否则{
如果(数据[索引[0]][索引[1]]==未定义){
数据[索引[0]][索引[1]]=数组(索引[2]+1).填充([]);
数据[索引[0]][索引[1]][索引[2]]=值;
}
}
}
控制台日志(数据)代码>此函数可能对您有所帮助
var data = []
setData(false, [1, 1, 3])
function setData(value_, indexes_) {
var currenLevelArr = data
var len = indexes_.length
// you can also use Foreach instead of using for
for (var i = 0; i < len; i++) {
if (currenLevelArr[indexes_[i]] === undefined) {
if (i === len - 1) {
// we meet the target
currenLevelArr[indexes_[i]] = value_
} else {
currenLevelArr[indexes_[i]] = []
currenLevelArr = currenLevelArr[indexes_[i]]
}
} else if (Array.isArray(currenLevelArr[indexes_[i]])) {
if (i === len - 1) {
// we meet the target but
//there is an extra dimension in the place
//in which we want to set the value_
console.error('something went wrong')
} else {
currenLevelArr = currenLevelArr[indexes_[i]]
}
} else {
// the current position is filled with sth that we dont expect
// if you want to replace the value ,you can add extra condition here
console.error('something went wrong')
}
}
}
console.log(data)
它不能很好地处理这些数据(数据结构和规则我认为是不同的。你能告诉我nodes
中的x,y
是什么吗?我假设数组的每个插槽都应该分配一次。我认为对于这个nodes
你应该使用objects。我将基于nodes
数据结构编写一个新函数。为什么你需要知道x,y参数ID是唯一涉及的,因为我将它拆分为维度,即1.1.2到[1,1,2]。
var nodes = [
{id: "1", x: 0, y: 0 },
{id: "1.1", x: 0, y: 0 },
{id: "1.1.1", x: 0, y: 0 },
{id: "1.2", x: 1, y: 0 },
{id: "1.3", x: 0, y: 1 },
{id: "1.4", x: 1, y: 1 },
{id: "1.3.1", x: 0, y: 0 },
{id: "1.3.2", x: 0, y: 1 }
];
data = [];
nodes.forEach(function(node_){
var indices = node_.id.split(".");
indices = indices.map(function(d_){ return Number(d_) - 1; })
setData(true, indices);
});
function setData(value_, indexes_) {
var currenLevelArr = data
var len = indexes_.length
for (var i = 0; i < len; i++) {
if (currenLevelArr[indexes_[i]] === undefined) {
currenLevelArr[indexes_[i]] = { value : undefined , subData : [] }
if (i === len - 1) {
currenLevelArr[indexes_[i]].value = value_ ;
} else {
currenLevelArr = currenLevelArr[indexes_[i]].subData;
}
} else {
if (i === len - 1) {
currenLevelArr[indexes_[i]].value = 10 ;
} else {
currenLevelArr = currenLevelArr[indexes_[i]].subData
}
}
}
}
function show(data , str="") {
if(Array.isArray(data) ){
for (var i = 0 ; i < data.length; i++ ) {
if(data[i]) {
if(data[i].value) {
console.log(str + (i+1) + ": true" );
}
show(data[i].subData , str + (i+1) + ",")
}
}
}
}
show(data)
console.log(data);