循环并在 javascript 中的数组对象键中使用条件基础创建编号

loop and create numbering with condition base in array object key in javascript

嗨,我正在尝试根据数组对象键条件创建这种循环模式, 如果 'o''t' 我们将从 'n' 开始索引 对象中的起点否则我们从主计数器继续编号

数据数组:

 let data = [
  {o:[],n:1}, //empty o could be our main counter this could also be dynamic or any number to start
  {o:[],n:1},
  {o:['t'],n:1}, //has t number start from n
  {o:['t'],n:1}, 
  {o:[],n:1}, // continue the last count of main counter
  {o:[],n:1},
  {o:['t'],n:1},
  {o:[],n:1},
  {o:[],n:1},
  {o:['t'],n:5},
  {o:['t'],n:5},
  {o:['t'],n:5}, 
  {o:[],n:1}, 
]

然后当我运行这个代码:

 recount(data){
   for (var i = 0; i < data.length; i++) {
     //code here 
     //new can also modify data[i].n
     console.log(**numbering**)
   }
 }

预期结果

//numbering
//1,2,1,2,3,4,1,5,6,5,6,7,7

感谢您的帮助

这是一个直接的循环,但它需要保留两个计数器:

  • 一个是普通计数器,用于 o 不包含 "t" 的项目,我们每次都会增加它。
  • 另一个是当 o 包含 "t" 时添加到 n 的偏移量。它从零开始。
    • 每符合条件的连续项目加一。
    • 如果我们不再遇到符合条件的项目,它会重置为零。

仅使用 ES5,这看起来像这样:

var data = [
  {o:[],n:1}, //empty o could be our main counter this could also be dynamic or any number to start
  {o:[],n:1},
  {o:['t'],n:1}, //has t number start from n
  {o:['t'],n:1}, 
  {o:[],n:1}, // continue the last count of main counter
  {o:[],n:1},
  {o:['t'],n:1},
  {o:[],n:1},
  {o:[],n:1},
  {o:['t'],n:5},
  {o:['t'],n:5},
  {o:['t'],n:5}, 
  {o:[],n:1}, 
]

function recount(data) {
  var counter = 1;
  var offsetT = 0;

  for (var i = 0; i < data.length; i++) {
    var item = data[i];
    if (item.o.indexOf("t") != -1) {
      item.n = item.n + offsetT++;
    } else {
      offsetT = 0;
      item.n = counter++;
    }
    console.log(item.n);
  }
}

recount(data);
console.log(data);

为了完整起见,ES6 版本的代码几乎相同:

var data = [
  {o:[],n:1}, //empty o could be our main counter this could also be dynamic or any number to start
  {o:[],n:1},
  {o:['t'],n:1}, //has t number start from n
  {o:['t'],n:1}, 
  {o:[],n:1}, // continue the last count of main counter
  {o:[],n:1},
  {o:['t'],n:1},
  {o:[],n:1},
  {o:[],n:1},
  {o:['t'],n:5},
  {o:['t'],n:5},
  {o:['t'],n:5}, 
  {o:[],n:1}, 
]

function recount(data) {
  let counter = 1; //let instead of var
  let offsetT = 0; //let instead of var

  for (let item of data) { //for..of instead of a normal for
    if (item.o.includes("t")) { //includes instead of indexOf
      item.n = item.n + offsetT++;
    } else {
      offsetT = 0;
      item.n = counter++;
    }
    console.log(item.n);
  }
}

recount(data);
console.log(data);

只是一个简单的算法,如果你需要帮助来理解它,请告诉我,我可以向你解释任何你不明白的地方

let data = [
    { o: [], n: 1 }, //empty o could be our main counter this could also be dynamic or any number to start
    { o: [], n: 1 },
    { o: ["t"], n: 1 }, //has t number start from n
    { o: ["t"], n: 1 },
    { o: [], n: 1 }, // continue the last count of main counter
    { o: [], n: 1 },
    { o: ["t"], n: 1 },
    { o: [], n: 1 },
    { o: [], n: 1 },
    { o: ["t"], n: 5 },
    { o: ["t"], n: 5 },
    { o: ["t"], n: 5 },
    { o: [], n: 1 },
  ];
  
  let numbering = 1;
  let secNumbering;
  let previous = false;
  
  function recount(data) {
    for (var i = 0; i < data.length; i++) {
      if (data[i].o.includes("t")) {
        if (previous) {
          secNumbering += 1;
          console.log(secNumbering);
          previous = true;
        } else {
          secNumbering = data[i].n;
          console.log(secNumbering);
          previous = true;
        }
      } else {
        console.log(numbering);
        numbering += 1;
        previous = false;
      }
    }
  }
  
  recount(data);