Blog

Array functions concat, copyWithin, every, fill, filter, find, forEach

1. concat function
merges arrays and returns the combined array
Example 1
var a = Array(1, 2, 3);
var b = Array(4, 5, 6);
var c=Array(7,8,9);
var d = a.concat(b, c);

a=1,2,3
b=4,5,6
c=7,8,9
d=1,2,3,4,5,6,7,8,9
concat does not modify the original arrays and we can concat as many arrays as required.

2. copyWithin function
copies elements inside the array
The copyWithin functions takes 1,2 or 3 parameters.
copyWithin(copytargetstart,copyelementsfromstart,stopcopyingfromhere)
The first parameter is required while others are optional.
copyelementsfromstart as default value 0, while
stopcopyingfromhere is the length of the array.

Example 1
var a = Array(1, 2, 3);
a.copyWithin(2);

a is 1,2,1. Value from position 0 (a[0]=10) has been copied to a[2].
Therefore array is 1,2,1

Example 2
var a=Array(1,2,3);
a.copyWithin(1);

a is 1,1,2

Example 3
var a = Array(1, 2, 3,4,5,6);
a.copyWithin(2,4);

a becomes 1,2,5,6,5,6
Copying starts at 2 and elements are copied starting from 4
a=1,2,3,4,5,6 so position 2 is replaced by element at position 4.
a=1,2,5,4,5,6
Next
position 3 is replaced by element at position 5.
a=1,2,5,4,5,6 becomes
a=1,2,5,6,5,6

Example 4
var a = Array(1, 2, 3,4,5,6);
a.copyWithin(1,2,4);
Elements are copied starting at destination 1, copying starts from position 2 and ends at 4.
Thus we have successively:
1,2,3,4,5,6 copy pos 2 to pos 1
1,3,3,4,5,6 copy pos 3 to pos 2
1,3,4,4,5,6 copy pos 4 to pos 3
1,3,4,5,5,6
1,3,4,4,5,6

 

 

every function
This function checks if every element passes a test.
The basic process is that you create a function with one parameter that is checked and the function returns true or false.
For instance:
This function checks for positive numbers and returns false if negative.
function check(x) {
x = Number(x);
if (x < 0)
return false;
else
return true;

}

Using this function we can check a whole array.

function f() {
var a = Array(1, 2, 3, 5, -3);
var result = a.every(function (x) { return check(x); });

}
function check(x) {
x = Number(x);
if (x < 0)
return false;
return true;
}

Value of result will be false in this case because there are negative numbers here.

function f() {
var a = Array(1, 2, 3, 5, 3);
var result = a.every(function (x) { return check(x); });
}
function check(x) {
x = Number(x);
if (x < 0)
return false;
return true;
}

Value of result will be true in this case because there are no negative numbers.

Fill function

The fill function takes three parameters:
First parameter is the value to be filled. No default value
Second where filling starts. Default value is 0
Third-1 where filling stops. Default value is array length-1

Example 1

function f() {
var a = Array(1, 2, 3,4,5,6);
a.fill(“pappu”);
}

This will change everything to apple.
Output: pappu,pappu,pappupappu,pappu,pappu

Example 2

function f() {
var a = Array(1, 2, 3,4,5,6);
a.fill(“pappu”,3);
}

Output: 1,2,3,pappu,pappu,pappu

Example 3

 

function f() {
var a = Array(1, 2, 3,4,5,6);
a.fill(“pappu”,3,5);
alert(a);
}

Output: 1,2,3,pappu,pappu,6

 

 Filter function
as the name suggests chooses those values which satisfy a given condition. the condition is expressed as a function.

 Example 1

function f() {
var a = Array(1, 2, 3, 4, 5, 6);
var b= a.filter(check);
}
function check(x) {
x = Number(x);
if (x %2== 0)
return true;
return false;
}
Output: b=1,2,3,4,5,6
and 
a=2,4,6

Example 2 
using anonymous function

function f() {
var a = Array(1, 2, 3, 4, 5, 6);
var b = a.filter(
function(x) {
x = Number(x);
if (x % 2 == 0)
return true;
return false;
}
);
}

Output: b=1,2,3,4,5,6
and 
a=2,4,6

Find function
find function returns the first instance of a value satisfying an condition
Example 1
search for the first even number

function f() {
var a = Array(1, 2, 3, 4, 5, 6);
var b= a.find(check);
}
function check(x) {
x = Number(x);
if (x % 2== 0)
return true;
return false;
}

Output: a=1,2,3,4,5,6
b=2

Example 2
If condition is never satisfied

function f() {
var a = Array(1, 2, 3, 4, 5, 6);
var b= a.find(check);
}
function check(x) {
x = Number(x);
if (x % 12== 0)
return true;
return false;
}

Output: a=1,2,3,4,5,6
b=undefined

Example 3

function f() {
var a = Array(1, 2, 3, 4, 5, 6);
var b= a.find(check);
alert(a);
if (b == undefined)
alert(“Not Found”);
else
alert(“Found”);
}

function check(x) {
x = Number(x);
if (x % 12== 0)
return true;
return false;
}

Output a=1,2,3,4,5,6
result Not Found

Example 4

function f() {
var a = Array(1, 2, 3, 24, 5, 6);
var b= a.find(check);
alert(a);
if (b == undefined)
alert(“Not Found”);
else
alert(“Found”);
}

function check(x) {
x = Number(x);
if (x % 12== 0)
return true;
return false;
}

Output: a=1,2,3,24,5,6
Result Found

 ForEach function
runs a for each loop

function traverse(value, i) {
alert( “position = ” + i + “, Value = ” + value);
}
function f() {
var a = Array(“Pappu”, 44, 5, -5, “Apple”);
a.forEach(traverse);
}

Output: position =
position = 0, Value = Pappu
position = 1, Value = 44
position = 2, Value = 5
position = 3, Value = -5
position = 4, Value = Apple