JavaScript在未预先定义尺寸的多维数组中设置值 [英] JavaScript Set value at multidimensional array where dimensions are not pre-defined
问题描述
假设我有一个空的1D数组:
Let's say I have an empty 1D array:
var data = [];
var data = [];
现在,我想将1的值添加到data [1] [1] [3];
Now, I want to add value of 1 to data[1][1][3];
为此,我需要将数据数组扩展为:
To do this, I need to extend data array to:
data = [
[],
[],
[[],[],[1]]
]
是的,非常确定我必须编写一个函数并将尺寸值作为1D数组[1、3、3]传递,并检查1.是否存在第二维,第三维,如果没有,则创建它们,然后检查2.它的大小大于或等于.
Yeah, pretty sure that I have to write a function and pass dimension values as 1D array [1, 1, 3] and check 1. if there is second, third dimension and if 'no' create them and 2. if its size are greater or equal.
对于这个例子,函数应该是
For just this example, function would be
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_;
}
}
}
笨拙而直截了当.我怎样才能使它变得通用呢? 对于任何尺寸的维度.
It's clumsy and straight-forward. How I can make an universal thing out of it? For any # of dimensions.
var data = [];
setData(false, [1, 1, 3]);
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_;
}
}
}
console.log(data);
推荐答案
此功能可能会对您有所帮助
this function might help you
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
对象的方式
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);
这篇关于JavaScript在未预先定义尺寸的多维数组中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!