0

I have a data of 82 items and i want to write 10 each in a file.How can i separate the array such that i get 10 each and 2 in another file.

MY code,

 var data  = [];
 data.forEach(function(i,v) {
   if((v != 0) && (v%10 === 0)) {
      var appendData = blogList.splice(0,10);
      fs.writeFile(appendData){

      }
   }

I have a data of 82 items and i want to write 10 each in a file.How can i separate the array such that i get 10 each and 2 in another file.

But I am getting the first 10 items in files.Can anyone help me.Thanks.

K L P
  • 107
  • 6

4 Answers4

1

Simple solution to this is :

let chunk = 10;
for (let i=0,j=blogList.length ; i<j ; i+=chunk) {
    let  appendData = blogList.slice(i,i+chunk);
    fs.writeFile(appendData){
        .....
    }
}

only the first 10 into the file

let  appendData = blogList.slice(0,10);
fs.writeFile(appendData){
    .....
}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
0

You have to get the new file each time you reach 10 elements. If the current index is not a multiple of 10 then you should simply add the value to the current file

var data = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
var myFile = "";//Get new file
data.foreach((x, i){
  if(i%10===0){
    myFile = ...//Get new file
  }
  myFile.writeFile(x){...}//Add the data to the file
});
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

With this code you will have a array with objects as {group: n, data:[]}, which you can use ng-repeat on the data

var app = angular.module("app", []);

app.controller("ctrl", ["$scope", function($scope) {

  var data = ["a", "b", "c", "d", "e", "f", "g", "h", "k", "l", "m", "n", "o", "r", "i", "q", "s", "t", "x", "z", "u", "hh"];

  $scope.array = [];
  var index = 1;
  while (data.length) {
    var object = {
      group: index++,
      data: []
    };
    object.data = data.splice(0, 10);
    $scope.array.push(object);
  }

}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container" ng-app="app" ng-controller="ctrl">
  <ul class="list-group" ng-repeat="object in array">
    <li class="list-group-item active">group {{object.group}}</li>
    <li class="list-group-item" ng-repeat="item in object.data">{{item}}</li>
  </ul>
</div>
Maher
  • 2,517
  • 1
  • 19
  • 32
0
Try this

var data = [];
for(var i=0;i<82;i++){
    data.push(i);
}
var chunkSize = 10;

var arr = data.reduce((acc, item, idx) => {
  let group = acc.pop();
  if (group.length == chunkSize) {
    acc.push(group);
    group = [];
  }
  group.push(item);
  acc.push(group);
  return acc;
}, [[]]);

console.log(arr);
Nair Athul
  • 823
  • 9
  • 21