-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"
    }

  ]
};
3
  • 1
    what means limit? index? please add the wanted result and what you have tried. Commented Jun 8, 2018 at 6:33
  • 2
    you can try array.slice() function, but it will create a new array. Commented Jun 8, 2018 at 6:34
  • Here is a working example : w3schools.com/jsref/tryit.asp?filename=tryjsref_slice_array Commented Jun 8, 2018 at 6:37

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.