在Javascript中如何在数组的每N个项中添加项

在Javascript中如何在数组的每N个项中添加项,javascript,arrays,add,Javascript,Arrays,Add,在初始阶段,我有一个对象数组: [ { type: 'all' }, { type: 'all' }, { type: 'all' }, { type: 'fruit'}, { type: 'all' }, { type: 'all' }, { type: 'fruit' }, { type: 'all' }, { type: 'all' }, { type: 'fruit' }, { type: 'fruit' } ] 我想插入: 1) 对象:{type:'toys'}

在初始阶段,我有一个对象数组:

[
 { type: 'all' },
 { type: 'all' },
 { type: 'all' },
 { type: 'fruit'},
 { type: 'all' },
 { type: 'all' },
 { type: 'fruit' },
 { type: 'all' },
 { type: 'all' },
 { type: 'fruit' },
 { type: 'fruit' }
]
我想插入:

1) 对象:{type:'toys'}在类型为:all的每5个项目中

2) 对象:{type:'Cloth'}在每2个类型为'fruit'的项目中

预期结果:

[
 { type: 'all' },
 { type: 'all' },
 { type: 'all' },
 { type: 'fruit'},
 { type: 'all' },
 { type: 'all' },
 { type: 'toys' },
 { type: 'fruit' },
 { type: 'clothes' },
 { type: 'all' },
 { type: 'all' },
 { type: 'fruit' },
 { type: 'fruit' },
 { type: 'clothes' }
]
如何实现此功能?我试图forEach添加项目,但在推送项目后,数组的demo长度发生了变化,很难测量列表中的下一个原始项目

array_demo.forEach((item, index) => {
  array_demo.push({type: 'demo'});
  console.warn(index);
});
您可以编辑数据,并使用两个外部计数器(
水果
,和
全部
)来计算每个计数器的出现次数。对于具有其中一种类型的每个对象,递增计数器,并检查它是否为第二个水果/5个全部。如果符合条件,则返回一个包含原始对象和添加对象的新数组。然后,按in展平所有子阵列

注意:该解决方案使用的目的是使其更通用

const data=[{“type”:“all”},{“type”:“all”},{“type”:“all”},{“type”:“all”},{“type”:“all”},{“type”:“fruit”},{“type”:“all”},{“type”:“all”},{“type”:“fruit”},{“type”:“fruit”},{“type”];
常量类型={
全部:{计数:0,大小:5,键入:'toys'},
水果:{计数:0,大小:2,类型:'Cloth'}
};
const result=[].concat(…data.map((o)=>{
常数to=类型[o.type];
return!to | |++to.count%to.size?o:[o,{type:to.type}];
}));

控制台日志(结果)您可以使用reduce并对所有的数据和结果进行计数

const数组\u演示=[
{type:'all'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'水果'}
]
设all=0;
让水果=0;
const newArray=array\u demo.reduce((acc,item,index)=>{
如果(item.type==='all'){
如果(全部===4){
acc.push(项目);
acc.push({type:'toys'})
all=0;
返回acc;
}否则{
全部++;
}
}else if(item.type==='fruit'){
如果(水果===1){
acc.push(项目);
acc.push({类型:'衣服'})
果实=0;
返回acc;
}否则{
水果++;
}
}
acc.push(项目);
返回acc;
}, []);

log(newArray)一种简单而传统的方法:

let init=[
{type:'all'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'水果'}
]
让last=[];
设allCounter=0;
设计数器=0;
for(让类型为init){
最后,推(型);
如果(type.type=='all'){
allCounter++;
如果(所有计数器==5){
last.push({type:'toys'})
allCounter=0;
}
}else if(type.type=='fruit'){
计数器++;
如果(计数器==2){
last.push({type:'cloth'})
水果计数器=0;
}
}
}

console.warn(last)
您可以计算发生次数,如果找到正确的出现次数,则插入,并将新对象添加到数组中

为了计算和保存逻辑,该方法使用一个对象,该对象具有所需的类型作为键,并使用所有其他值,例如用于插入的
size
type

var数组=[{type:'all'},{type:'all'},{type:'all'},{type:'fruit'},{type:'all'},{type:'fruit'},{type:'all'},{type:'fruit'},{type:'fruit'},{type:'fruit'},
类型={all:{count:0,size:5,类型:'toys'},水果:{count:0,size:2,类型:'costs'},
类型,
i=0;
while(i

.as控制台包装{max height:100%!important;top:0;}
其他解决方案,结果相同

function insertion(my_array){
    var i=0,
        j=0,
        n=my_array.length,
        k=0;
    while(k<n){
        if(my_array[k].type=='all'){
            i++;console.log(i);
        }else if(my_array[k].type=='fruit'){
            j++;console.log(j);
        }
        if(i==5){
            k++;
            my_array.splice(k,0,{ type: 'toys' });
            i = 0;
            n++;
        }else if(j==2){
            k++;
            my_array.splice(k,0,{ type: 'clothes' });
            j = 0;
            n++;
        }
        k++;
    }
    return my_array;
}
函数插入(我的数组){
var i=0,
j=0,
n=我的数组长度,
k=0;
而(k
var数据=
[
{type:'all'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'all'},
{type:'all'},
{type:'水果'},
{type:'水果'}
];
var结果=[];
var-allCounter=0;
var-counter=0;
data.forEach(函数(项、索引){
结果:推送(项目);
如果(item.type==“全部”){
allCounter++;
}
如果(item.type==“水果”){
计数器++;
}
如果(所有计数器==5){
结果。推送({“类型”:“玩具”});
allCounter=0;
}
如果(计数器==2){
结果.推送({“类型”:“衣服”})
水果计数器=0;
}
});

console.log(result);
解决了类似的需求:

const concatAt=(数组、谓词、值)=>array.reduce(
(c,n,idx)=>{
const at=typeof谓词===“函数”?谓词(idx,n,数组):谓词===idx
const newValue=typeof value==“函数”?值(n,idx,数组):值
返回时间?[…c,n,newValue]:[…c,n]
} , [])
常量IDodIndex=n=>n%2
常量isNth=n=>n=>n%n===n-1
常数增量=n=>n+1
log(concatAt([0,1,3,4,6],idodIndex,increment))/[0,1,2,3,4,5,6]
concatAt([0,1,3,4,6],第n(3),增量)/[0,1,3,4,4,6]

我希望它能帮助别人。

在另一个阵列上执行,而不是在原地执行
function insertion(my_array){
    var i=0,
        j=0,
        n=my_array.length,
        k=0;
    while(k<n){
        if(my_array[k].type=='all'){
            i++;console.log(i);
        }else if(my_array[k].type=='fruit'){
            j++;console.log(j);
        }
        if(i==5){
            k++;
            my_array.splice(k,0,{ type: 'toys' });
            i = 0;
            n++;
        }else if(j==2){
            k++;
            my_array.splice(k,0,{ type: 'clothes' });
            j = 0;
            n++;
        }
        k++;
    }
    return my_array;
}