您可以使用以下代码来实现这两个操作:

var array1 = ['DTCode 5000', 'TxtID 1369', 'DTCode 6313', 'TxtID 1370', 'DTCode 9020', 'TxtID 1162', 'TxtID 11451', 'DTCode 9643', 'TxtID 1357', 'DTCode 9645', 'TxtID 1358'];

var array2 = [];
var currentRow = [];

for (var i = 0; i < array1.length; i++) {
  var element = array1[i];
  
  if (element.includes('DTCode')) {
    currentRow.push(element);
  } else if (element.includes('TxtID')) {
    currentRow.push(element);
    
    if (currentRow.length > 1) {
      array2.push(currentRow);
      currentRow = [];
    }
  }
}

if (currentRow.length > 0) {
  array2.push(currentRow);
}

console.log(array2);

这段代码首先定义了一个空数组array2和一个空数组currentRow,用于存储结果。

然后,使用for循环遍历array1数组。对于每个元素,如果它包含"DTCode",则将其添加到currentRow数组中。如果它包含"TxtID",则将其添加到currentRow数组中,并将currentRow数组添加到array2数组中。同时,将currentRow数组重置为空数组,以便开始下一行的构建。

最后,如果currentRow数组中还有元素,说明最后一行没有带有DTCode的元素,需要将其添加到array2数组中。

最后,打印array2数组,即为所需的结果。

var array1 DTCode 5000 TxtID 1369 DTCode 6313 TxtID 1370 DTCode 9020 TxtID 1162 TxtID 11451 DTCode 9643 TxtID 1357 DTCode 9645 TxtID 1358用js进行如下操作1遍历数组1将遍历到带有DTCode的元素和带有TxtID的元素放在数组2的同一行直到遍历到另一个带有DTC

原文地址: http://www.cveoy.top/t/topic/jcV9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录