Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Friday, December 28, 2012

Published 7:02 AM by with 1 comment

JavaScript Array Functions

Javascript Array Functions (Array)


var fruit = ["apple","orange","banana"];
log(fruit.sort());//apple,banana,orange
fruit.push("pear");
log(fruit.sort());//apple,banana,orange,pear

var vegitables = ["carrot","broccoli","cauliflovd"];
var all = fruit.concat(vegitables);
log(all);//apple,orange,banana,carrot,broccoli,cauliflovd
 
QRCode

Javascript Array Functions (Array)


Javascript Array functions
push,pop,concat,map,filter,some,every,forEach,reduce,sort,splice,slice,join,reverse
Array is a Stack data sructure (first in first out)

Example

var fruit = ["apple","orange","banana"];
log(fruit.sort());//apple,banana,orange

fruit.push("pear");
log(fruit.sort());//apple,banana,orange,pear

var vegitables = ["carrot","broccoli","cauliflovd"];
var all = fruit.concat(vegitables);
log(all);//apple,orange,banana,carrot,broccoli,cauliflovd

var firt = fruit.slice(0,1);
log(first);//apple

var result = fruit.splice(1,2,"melon","grape");
log(result);//start in 1 no of 2 remove and add "melon","grape"
//out put is orange,banana
//splice is immutable function in javascript

fruit.splice(1,2,"melon","grape");
log(fruit);
//apple,melon,grape

fruit = fruit.map(function(i){return i.toUpperCase()});
log(fruit);
//APPLE,MELON,GRAPE

fruit = fruit.map(function(i){return {fruitname:i}});
log(fruit);
//[object Object],[object Object],[object Object]

fruit = fruit.filter(function(){
    return i[0]==="a";
});
log(fruit);
//apple

fruit = fruit.every(function(){
    return i[0]==="a";
});
log(fruit);
//false //if every items start with "a"


fruit = fruit.every(function(){
    return i.length>0;
});
log(fruit);
//true //if every items has more than 0 length

fruit = fruit.some(function(){
    return i.length>0;
});
log(fruit);
//true //if some items has more than 0 length

fruit.forEach(function(){
    log(i);
});
Read More
    email this       edit