I'm currently going through array exercises to practice my programming on array iteration. I have an array of string objects inside that look like this.
var balling = [
"Calvin Klein", "Tommy Hilfiger", "FUBU", "Rocca Wear",
"Calvin Klein", "Tommy Hilfiger", "FUBU", "Rocca Wear"
];
Now what I know will work is if I come in and loop through all of them like this.
for (var i = 0; i < balling.length; i++) {
balling[i] = console.log(balling[i]);
};
and then the whole thing prints out one by one, top to bottom.
What I would rather do is instead of all 8 of the objects inside that array printing out, I want the for to specify a specific range of objects inside my array.
What exactly do I have to do for my for loop to get the result I want? Is there a way for me to specify how many objects in my array get printed out? Not just one but two, three, and a starting place to start the array and a set range?
You can use
Math.min(a,b)function, that gets the lowest value fromaorb, so if the array length is smaller than the number of entries you want to get as minimal, you will not have a problem:If you want a differente range, you can also use
Array.prototype.slicemethod: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice?v=example