Array is very common method in javascript so today i am going to show you some Array method. let's start
let fruits = ['banana', 'apple', 'kiwi', 'strawberry']; fruits = new Array('banana', 'apple', 'kiwi', 'strawberry'); console.log(fruits); console.log(fruits[4]); // if write console.log(fruits[3]); you shall see straberry but now undifined. fruits[fruits.length] = 'mango' console.log(fruits) // now you had see mango has added last of the list. console.log(fruits.join(' - ')); // used for add somthing between fruit console.log(fruits.pop(), fruits) // remove last item console.log(fruits.push('respberry')) // appends console.log(fruits.sort()); console.log(fruits.reverse()); fruits.shift(); // for remove first item console.log(fruits); fruits.unshift('guava'); // for added in first console.log(fruits); // combine in javascript. let groceries = ['onion', 'tomato', 'broccoli'] allGroceries = (fruits.concat(groceries)); console.log(allGroceries); console.log(allGroceries.sort()); console.log(allGroceries.slice(2, 5)) // some item sliced let someNumbers = [43, 23, 45, 654, 32, 77, 99, 22, 443]; someNumbers = new Array(43, 23, 45, 654, 32, 77, 99, 22, 443) console.log(someNumbers); console.log(someNumbers.sort(function(a, b) {return a-b})); console.log(someNumbers.sort(function(a, b) {return b-a})); let emptyArray = new Array(); for (let x = 0; x <= 10; x++) { emptyArray.push(x) } console.log(emptyArray)
thanks for try with me and many way to do array i hope it is easy next post coming soon
No comments:
Post a Comment