JavaScript 在未预定义维度的多维数组中设置值

JavaScript Set value at multidimensional array where dimensions are not pre-defined

假设我有一个空的一维数组:

var 数据 = [];

现在,我想将值 1 添加到数据[1][1][3];

为此,我需要将数据数组扩展为:

data = [

 [],
 [],
 [[],[],[1]]

]

是的,很确定我必须编写一个函数并将维度值作为一维数组 [1, 1, 3] 传递并检查 1。如果有第二个、第三个维度,如果 'no' 创建它们和 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 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);

这个功能可能对你有帮助

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);