[Solved] Add an object inside an array of objects every X objects


You can use data.length % 20 === 0 on each retrieval of data or in a loop, according to how you want the new object to be inserted. Using modulus for 20 and comparing with 0 will add the new object when length is 20, 40, 60, ....

var data = [

 {_id: "59b6433532b60cfc0a253cec", description: "", imagepath: 
 "https://xxx.1", likes: 0, downvotes: 0},

 {_id: "59b6433532b60cfc0a253ceb", description: "", imagepath: 
 "https://xxx.2", likes: 0, downvotes: 0},

 {_id: "59b6433532b60cfc0a253cea", description: "", imagepath: 
 "https://xxx.3", likes: 0, downvotes: 0}
];

var modCount = 0;
var objToInject= {type: 'blog_post'};

//after each retrieval of the JSON array as in data, check if length is 20,40,60,... and so on
if((data.length - modCount) % 20 === 0){
  data.push(objToInject);
  modCount++;
}

console.log(data);

4

solved Add an object inside an array of objects every X objects