-1

I have startLimit=2 and endlimit=4 so i need to get only records from 2 to 4 in Items array with out using for loop

var favorites = {
  "userID": "12345678",
  "Items": [{
      "productID": "11234567",
      "added": "TIMESTAMP",
      "title": "Project",
      "type": "Weekend Project",
      "imageURL": "1"
    },

    {
      "productID": "11223456",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },

    {
      "productID": "11223345",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223721",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "1122456",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223734",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "11224566",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    }

  ]
};
str
  • 42,689
  • 17
  • 109
  • 127
dinesh
  • 11
  • 1
  • 5

4 Answers4

0

var yourNewArray = favorites.Items.slice(1,4);
console.log(yourNewArray);

parameters 1 and 4 because index of slice starts from 0 , so second element would be 1 and fourth element would be 3 but in slice it excludes last hence 4.

prady
  • 416
  • 2
  • 6
  • 17
0

You can use Array.prototype.slice to get range of items from an array as below:

var favorites = {
  "userID": "12345678",
  "Items": [{
      "productID": "11234567",
      "added": "TIMESTAMP",
      "title": "Project",
      "type": "Weekend Project",
      "imageURL": "1"
    },

    {
      "productID": "11223456",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },

    {
      "productID": "11223345",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223721",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "1122456",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223734",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "11224566",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    }

  ]
};

var sliced = favorites.Items.slice(1, 4); 

console.log(sliced);
Isaac
  • 12,042
  • 16
  • 52
  • 116
0

To get the 2 to 4 elements of an array, use

var favorites = {
  "userID": "12345678",
  "Items": [{
      "productID": "11234567",
      "added": "TIMESTAMP",
      "title": "Project",
      "type": "Weekend Project",
      "imageURL": "1"
    },

    {
      "productID": "11223456",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },

    {
      "productID": "11223345",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223721",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "1122456",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223734",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "11224566",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    }

  ]
};

var arr = favorites.Items;

var result = arr.slice(2, 4);

console.log(result);
dekts
  • 744
  • 4
  • 19
0

Use slice():

var favorites = {
  "userID": "12345678",
  "Items": [{
      "productID": "11234567",
      "added": "TIMESTAMP",
      "title": "Project",
      "type": "Weekend Project",
      "imageURL": "1"
    },

    {
      "productID": "11223456",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },

    {
      "productID": "11223345",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223721",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "1122456",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    },
    {
      "productID": "11223734",
      "added": "TIMESTAMP",
      "title": "Bathroom",
      "type": "Weekend Project",
      "imageURL": "2"
    },
    {
      "productID": "11224566",
      "added": "TIMESTAMP",
      "title": "Curves",
      "type": "Collections",
      "imageURL": "3"
    }

  ]
};
var startLimit = 2, endLimit = 4;
var res = favorites.Items.slice(startLimit,endLimit);
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62