-4

In javascript I have an array of something like 500,000 items.

I want to send to server 1000 items multiple times.

Seems that I need to use the Slice Function.

How can I do it send 1000 items each time and the last time send the last items. either if it less than 1000 items.

24sharon
  • 1,859
  • 7
  • 40
  • 65
  • Sorry for being little rude, but looking at your rep, its obvious you have been here for sometime, so you should know how to ask question and what is the importance of sharing *code/effort* in question – Rajesh Feb 03 '17 at 07:37
  • An example of creating chunks with the slice function can be found here: http://stackoverflow.com/questions/8495687/split-array-into-chunks – Me.Name Feb 03 '17 at 07:37
  • Have no idea what you are asking for, a List<> with 1000 item inside send each time? or what is the object format required by the API? – PSo Feb 03 '17 at 07:39

1 Answers1

0
var splicedItems = [];

        for(var i =0; i<=yourBigArr.length-1; i++)
        {
          splicedItems[] = yourBigArr.splice(0,1000);
        }

splice is used to modify your array. In the above case we are splicing 1000 items and the each time the array would be reducing by 1000 items.So the index would always start from 0 and the items to chop would be 1000(as you have stated)

M3ghana
  • 1,231
  • 1
  • 9
  • 19
  • WITH SOME CHANGES var loop = function () { $scope.model.ids = $scope.ids.splice(0, $scope.each); exportService._import($scope.model).then(function (response) { $scope.response = response; if ($scope.ids.length > 0) { loop(); } }); – 24sharon Feb 04 '17 at 19:18