xxxxxxxxxx
let str = "12345.00";
str = str.substring(0, str.length - 2);
console.log(str);
xxxxxxxxxx
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
xxxxxxxxxx
const numbers = [1, 2, 3, 4, 5];
for (i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
xxxxxxxxxx
let colors = ['red', 'green', 'blue'];
for (const color of colors){
console.log(color);
}
xxxxxxxxxx
/* new options with IE6: loop through array of objects */
const people = [
{id: 100, name: 'Vikash'},
{id: 101, name: 'Sugam'},
{id: 102, name: 'Ashish'}
];
// using for of
for (let persone of people) {
console.log(persone.id + ': ' + persone.name);
}
// using forEach(...)
people.forEach(person => {
console.log(persone.id + ': ' + persone.name);
});
// output of above two methods
// 100: Vikash
// 101: Sugam
// 102: Ashish
// forEach(...) with index
people.forEach((person, index) => {
console.log(index + ': ' + persone.name);
});
// output of above code in console
// 0: Vikash
// 1: Sugam
// 2: Ashish
xxxxxxxxxx
var numbers = [22, 44, 55, 66, 77, 99];
for (var i = 0; i < numbers.length; i++) {
var num = numbers[i]
console.log(num)
}
//Output: 22,44,55 66 77 99
xxxxxxxxxx
var arr = [1, 2, 3, 4, 5];
for (var i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}
xxxxxxxxxx
var min = arr[0];
var max = arr[0];
for(var i=1; i<arr.length; i++){
if(arr[i] < min){
min = arr[i];
}
if(arr[i] > max){
max = arr[i];
}
return [min, max];
}
xxxxxxxxxx
let arr = [1, 2, 3, 4]
for (let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
xxxxxxxxxx
var arr = ['a', 'b', 'c'];
arr.forEach(item => {
console.log(item);
});