RcBuilder@walla.com
call centre: 054-5614020

javascript arrays

26
Jan
2016
Posted by: RcBuilder  /   Category: Array / JAVASCRIPT / OOP   /   No Comments

javascript arrays

structure:

- var arr = [];
- var arr = new Array();

example:

var arr = [1,2,3,4,5,6,7,8];

data types:

arrays can hold any type of objects, not necessarily the same for all organs
they can be consisted of mixed types (string, int, function, custom objects etc.)

example:

var arr = [
'hello world',
23,
400.31,
function (name) { console.log('hello again ' + name); },
function () { console.log('hello there'); },
{ x: 30, y: 50, z: 70 }
];

console.log(arr[0]); // hello world
console.log(arr[1]); // 23
console.log(arr[2]); // 400.31

var fun = arr[3];
fun(‘Roby’); // this will log ‘hello again Roby’ to the console

arr[4](); // this will log ‘hello there’ to the console
console.log(arr[5].z); // 70

access to values:

- use push, pop and shift methods
- use push method to add values to an array
- use pop method to get + remove out values from the array (LIFO)
- use shift method to get + remove out values from the array (FIFO)
- use index to get value from an array (without remove it)

example:

var arr = new Array();
arr.push(1);
arr.push(2);
arr.push(3);

console.log(arr.length); // 3

console.log(arr[1]); // 2

console.log(arr.pop()); // 3
console.log(arr.pop()); // 2
console.log(arr.pop()); // 1

console.log(arr.length); // 0

add value to specific index:

we can add some value to a specific location within an array,
notice that if we’ll add to an index higher than exists, javascript will add all the rest organs with undefined values

example:

var arr = new Array();
arr[4] = 40; // index 4 (0 based)

console.log(arr[2]); // undefined – the same for organs 0,1,2,3
console.log(arr[4]); // 40
console.log(arr.length); // 5 – the array length is 5!

named index:

notice! there’s no such thing!!
this technique will refer the Array as an object with properties and all the Array built-in methos will not work properly!
see example below (arr.length returns 0)

tip: use object instead – e.g: var dictionary = {};
see also ‘object properties access using []’

example:

var arr = new Array();
arr['Roby'] = { Id: 1, Name: ‘Roby Cohen’, Age: 35 };
arr['Avi'] = { Id: 10, Name: ‘Avi Cohen’, Age: 33 };
arr['Shirly'] = { Id: 100, Name: ‘Shirly Cohen’, Age: 36 };

console.log(arr.length); // 0 !!!

console.log(arr['Roby']); // [object Object]
console.log(arr[1]); // undefined
console.log(arr['Shirly'].Id); // 100

- best practice
as we can see above, we can defined an array using named index for easier access later on but javascript
will refer the array as an object with properties(Roby, Avi etc.), therefore it’s recommended to use a custom object instead.

var users = new Object(); // can use {} instead
users['Roby'] = { Id: 1, Name: ‘Roby Cohen’, Age: 35 };
users['Avi'] = { Id: 10, Name: ‘Avi Cohen’, Age: 33 };
users['Shirly'] = { Id: 100, Name: ‘Shirly Cohen’, Age: 36 };

console.log(users['Roby']); // [object Object]
console.log(users['Shirly'].Id); // 100
console.log(users.Roby); // [object Object]
console.log(users.Shirly.Id); // 100

Author Avatar

About the Author

בניית אתרים ופתרונות טכנולוגים | RcBuilder

No Comments


  • פיתוח מערכות
  • פתרונות טכנולוגים
  • קידום אתרים
  • בניית אתרים