Handling the JavaScript Array Object

Initializing an Array

There are 3 ways to initialize an array
1-
var shapes = new Array(); //Optional parameter array size
shapes[0]= "Square";
shapes[1]= "Rectangle";
shapes[2]= "Triangle";

2-
var shapes = new Array("Square","Rectangle","Triangle");
3-
var shapes = ["Square","Rectangle","Triangle"];
There is no performance impact in choosing any of these.(Whether you are using primitive or complex types)

The difference between the three ways is that; in the first way, the constructor can take a parameter with the length of the array, and the length right after initialization will be the value specified even if the array has no objects yet.

var shapes = new Array(2);
//the value of shapes.length is 2
shapes[0]= "Square";
shapes[1]= "Rectangle";
shapes[2]= "Triangle";
//The array will not raise an exception as expected
//it will just increase the length of the array
//the value of shapes.length becomes

Manipulating objects in array :

1- Push: Adds the value to the end of the array and returns the length of the array after adding the element
var letters = ["a", "b", "c"];
//array has a,b,c
letters.push("d");
//array now is a,b,c,d
letters.push("e","f");
//array now is a,b,c,d,e,f

2- Unshift: Adds the value to the beginning of the array. returns the length of the array after adding the element
var letters = ["a", "b", "c"];
//array has a,b,c
letters.unshift("d");
//array now is d,a,b,c
letters.unshift("e","f");
//array now is e,f,a,b,c,d

3- Pop: removes the last element of the array. returns the object removed.
var letters = ["a", "b", "c"];
//array has a,b,c
letters.pop();
//array now is a,b
//note that if u add any parameter to the pop method it will be ignored
//without giving any errors

4- Shift: removes the first element of the array. returns the object removed.
var letters = ["a", "b", "c"];
//array has a,b,c
letters.shift();
//array now is b,c
//note that if u add any parameter to the shift method it will be ignored
//without giving any errors

5- Splice:
takes parameters (StartIndex, DeleteCount, [Object1,…]) ; a very useful method that can do multiple things.

  • Adding object
  • Removing object(s)
  • Replacing object(s)

var letters = ["a", "b", "c"];
//array has a,b,c
letters.splice(letters.length,0,"d");
//element is added to the end of the array
//array value is a,b,c,d
letters.splice(0,0,"-1");
//element is added to the beginning of the array
//array value is -1,a,b,c,d
letters.splice(0,0,"-3","-2");
//as noticed the splice can accept multiple objects to be added
//elements are added to the beginning of the array
//array value is -3,-2,-1,a,b,c,d
letters.splice(10,0,"m");
//element is added to the beginning of the array
//array value is -1,a,b,c,d,m
//giving an out of range value will not raise errors
//it will just add the specified object to the end of the array
letters.splice(1,2);
//elements are removed from the middle of the array
//array value is -3,a,b,c,d,m
letters.splice(0,1);
//element is removed from the beginning of the array
//array value is a,b,c,d,m
letters.splice(3,2);
//element is removed from the end of the array
//array value is a,b,c
letters.splice(1,1,"x","y");
//The element "b" at position 1 is removed and
//replaced with "x","y"beginning of the array
//the array value is a,x,y,c

Advertisements

Sharepoint 2010 Configuration Wizard Error: System.ServiceProcess.TimeoutException: Time out has expired and the operation has not been completed.

While creating a VPC, I installed Windows Server 2008 R2, Visual Studio 2010 RC  and then the Sharepoint 2010 RC.  I stumbled upon the following error while running the Sharepoint 2010 Configuration Wizard “Error: System.ServiceProcess.TimeoutException: Time out has expired and the operation has not been completed.” It occurred twice on Configuration task 5 of 10; the first time when the status was “Successfully installed service instance: Sharepoint Server Search.”


Sharepoint Server Search

Sharepoint Server Search

In order to fix this error, I opened the Services, changed the log on of the service named “Sharepoint Server Search V14” from local service to local system and started the service manually while the wizard was still running.

The second time when the status was “Successfully installed service instance: Sharepoint Foundation Search”.

Sharepoint Foundation Search

Sharepoint Foundation Search

This time I changed the log on of the service named “Sharepoint Foundation Search V4” from local service to local system and started the service manually.

Afterwards, the configuration wizard completed successfully.

Error: Installation failed for component Microsoft Visual Studio 2010 64bit Prerequisites

When trying to download Visual Studio 2010 RC, I ran the setup but the installer threw the following error

“VC 9.0 Runtime (x86) : [2] Error : Installation failed for component VC 9.o RUntime (x86). MSI returned error code 1603”

I removed all VC Components from the programs and features. Another error popped up

“Microsoft Visual Studio 2010 64bit Prerequisites (x64) : [2] Error: Installation failed for component Microsoft Visual Studio 2010 64bit Prerequisites (x64)”

After inspecting the error log i found the error in the .Net framework 4.0 machine.config i thought that error occurred because the installer did some action while installing the VC 9.0 Runtime and it was not rolled back in the machine.config . I removed all components installed by the Visual Studio 2010, started a clean installation and It worked just fine.