如何在多维数组循环中的特定位置插入项目

3

我正在循环遍历以下的二维数组,并且对于每个子数组中的第一个元素,我需要在除第一个和可能的最后一个元素以外的每个元素之后插入一个对象。数据看起来像这样...

const data = [
  ['Path', 'Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Score'],
  ['/path-one', 1, 3, 2, 2, 4,  3],
  ['/path-two', 4, 5, 5, 5, 6, 3],
  ['/path-three', 5, 5, 3, 5, 3, 3],
  ['/path-four', 2, 3, 4, 2, 2, 3],
]

在第一行中,除了第一个和最后一个项目外,我想在每个其他项目之后插入一个对象{role: 'annotation'}。对于其余的行,对于第一个项目数组中具有角色对象的每个索引,我想复制上一个值,使得修改后的第一个数组为: ['Path', 'Item1', {role: 'annotation'}, 'Item2', {role: 'annotation'}, 'Score'],然后其他数组将按照模式['/path-one', 'value1', 'value1', 'value2', 'value2', 'value3']进行。

到目前为止,我的解决方案是不足的。以下是我想到的...

let indexes = []
let scoreIndex
const result = data.map((item, index) => {
  let clone = [...item]
  item.map((i, ix) => {
    if (index === 0) {
      if (ix !== 0 && typeof clone[ix + 1] === 'string' && typeof clone[ix] !== 'object') {
        if (clone[ix + 1] === 'Score') {
          scoreIndex = ix
        }
        indexes.push(ix)
        clone.splice((ix + 1), 0, {role: 'annotation'})
      }
      return i
    } else {
      if (indexes.includes(ix) && ix !== scoreIndex) {
        item.splice((ix + 1), 0, i)
        return i
      }
      return i
    }
  })
  return clone
})

非常感谢您的帮助。

2个回答

1
这个答案并不完美,但应该能给出如何解决此问题的想法。
  const data = [
  ['Path', 'Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Score'],
  ['/path-one', 1, 3, 2, 2, 4,  3],
  ['/path-two', 4, 5, 5, 5, 6, 3],
  ['/path-three', 5, 5, 3, 5, 3, 3],
  ['/path-four', 2, 3, 4, 2, 2, 3],
]
  let indexes = []
let scoreIndex

// This method takes in an error and perform splice with the value passed, if no value passed, it will use the current index value.
const splicer = (arr, index, value) => {
  var length = (arr.length - 1) * 2;
  for(let i = 1; i < length; i = i+2) {
    if(value) {
      arr.splice(i, 0, value);
    } else {
      arr.splice(i, 0, arr[i]);
    }
  }
  return arr;
}

// This method goes through data (array of arrays) and call splicer
const output = data.map((item, index) => {
  if(index == 0) {
    splicer(item, 0, {role : "annotation"})
  } else {
    splicer(item, 0);
  }
});

输出

["Path",{"role":"annotation"},"Item1",{"role":"annotation"},"Item2",{"role":"annotation"},"Item3",{"role":"annotation"},"Item4",{"role":"annotation"},"Item5",{"role":"annotation"},"Score"]
["/path-one",1,1,3,3,2,2,2,2,4,4,3,3]
["/path-two",4,4,5,5,5,5,5,5,6,6,3,3]
["/path-three",5,5,5,5,3,3,5,5,3,3,3,3]
["/path-four",2,2,3,3,4,4,2,2,2,2,3,3]

使用map函数跳过某些值有一定难度(可能是可行的),因此我使用了计划循环。

1
也许我理解错了什么。我假设以下内容:
:: 数据数组中每个数组的长度相同。
:: 您需要在每个位置后插入,这将破坏增加其(迭代)变量的for循环。
:: 第一个数组中始终存在路径分数
:: 除上述两个值外,您希望在每个值后添加一个新值。 解决方案
  1. 获取每个数组的长度:data [0] .length
  2. 从末尾开始循环。
  3. 忽略最后一个位置。
  4. 忽略第一个位置
  5. 根据它是否是数据数组中的第一项,切换不同的值。

我遍历每个值一次,但按以下顺序进行:'Item5',4,6,3,3,'Item4',2,5,5,2,'Item3',...

const data = [
  ['Path', 'Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Score'],
  ['/path-one', 1, 3, 2, 2, 4,  3],
  ['/path-two', 4, 5, 5, 5, 6, 3],
  ['/path-three', 5, 5, 3, 5, 3, 3],
  ['/path-four', 2, 3, 4, 2, 2, 3],
]

function mapArray(data) {
  let secondToLastItem = data[0].length - 2; // 1 & 3
  let FIRST_ITEM = 0; // 4

  for (let index = secondToLastItem; index > FIRST_ITEM; index--) { // 2
    for (let item = 0; item < data.length; item++) {
      let injectedValue = (item == FIRST_ITEM)  // 5
          ? {'role': 'annotation'}
          : data[item][index]; 
      
      data[item].splice(index + 1, 0, injectedValue);
    }
  }
  
  return data;
}

console.log( mapArray(data) );


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接