[英]How to insert item at certain positions in a multiple array loop
I'm looping over the following array of arrays and for the first item in the array, I need to insert an object after every item, except the first and maybe last item.我正在遍历以下数组数组,对于数组中的第一个项目,我需要在每个项目之后插入一个对象,除了第一个可能是最后一个项目。 The data looks like this...
数据是这样的...
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],
]
For the first row, except the first item and last item, after every other item I want to insert an object {role: 'annotation'}
.对于第一行,除了第一项和最后一项,我想在每个其他项目之后插入一个对象
{role: 'annotation'}
。 For the rest of the rows, for every index in the first item array that has the role object, I want to duplicate the previous value such that if the first array after modification is: ['Path', 'Item1', {role: 'annotation'}, 'Item2', {role: 'annotation'}, 'Score']
, then the other arrays will follow the pattern ['/path-one', 'value1', 'value1', 'value2', 'value2', 'value3']
对于其余的行,对于具有角色对象的第一个项目数组中的每个索引,我想复制以前的值,如果修改后的第一个数组是:
['Path', 'Item1', {role: 'annotation'}, 'Item2', {role: 'annotation'}, 'Score']
,然后其他数组将遵循模式['/path-one', 'value1', 'value1', 'value2', 'value2', 'value3']
My solution so far has been inadequate.到目前为止,我的解决方案是不够的。 Here's what I came up with...
这是我想出的...
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
})
Your help would be greatly appreciated.您的帮助将不胜感激。
This answer is not perfect, but should give an idea on how this can solved.这个答案并不完美,但应该给出如何解决这个问题的想法。
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);
}
});
Output输出
["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]
With map, it is a little tough to skip values (it should be possible), so have used a plan for loop.使用地图,跳过值有点困难(应该是可能的),因此使用了循环计划。
Perhaps I misunderstood something.也许我误解了一些东西。 I assumed the following:
我假设如下:
:: The lengths of each array in the data array are the same. :: data数组中每个数组的长度都是一样的。
:: You need to inject after each position, which will mess up a for loop that increase it's i
(teration) variable. :: 你需要在每个位置之后注入,这会弄乱一个 for 循环,增加它的
i
(teration) 变量。
:: There is always a Path
and a Score
in the first array. :: 第一个数组中总是有一个
Path
和一个Score
。
:: You want to add a new value after each value, apart from the two above. :: 除了上面两个值之外,您想在每个值之后添加一个新值。
Solution解决方案
data[0].length
data[0].length
I loop over each value once, but I do it in the order of: 'Item5', 4, 6, 3, 3, 'Item4', 2, 5, 5, 2, 'Item3', ...我将每个值循环一次,但我按以下顺序执行:'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) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.