[英]Insert Data into a Array periodically JS(react native)
I have a simple array which contains objects like the following - 我有一个简单的数组,其中包含如下对象:
Array:{
o1:{
}
o2:{
}
o3:{
}
......
o100:{
}
}
So here I have an object - 所以我这里有一个对象-
OBJECT:{
}
Which I need to insert into the array periodically - like after every 5 items the new object should be inserted. 我需要定期将其插入到数组中-像每5个项目之后应插入一个新对象。
So how should I insert it into the array? 那么我应该如何将其插入数组? The data in the existing index should not be overwritten.
现有索引中的数据不应被覆盖。
Anyone can give me a hint? 有人可以给我提示吗?
Thanks. 谢谢。
It depends on the use case but it looks like you want to use the splice
[javascript array method]. 这取决于用例,但看起来您想使用
splice
[javascript数组方法]。 If you have your complete Array
object, you can loop over it with something like this: 如果您有完整的
Array
对象,则可以使用以下方法对其进行循环:
const step = 5;
for (var i = step; i < yourArrayObject.length; i += step) {
yourArrayObject.splice(i, 0, objectToInsert)
}
This will take your completed Array
object and insert the object every step
places. 这将获取您完成的
Array
对象,并在每个step
插入该对象。 Be aware that this code will mutate the original Array
object. 请注意,此代码将使原始
Array
对象发生变异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.