Showing posts with label ArraySearch. Show all posts
Showing posts with label ArraySearch. Show all posts

Thursday, April 5, 2018

Array Reordering - map


                      This method is really very useful and often time we will use in our project.

It calls the function for each element of the array and returns the array of results. It does not change the original array.

Syntax:

let result = arr.map(function(item, index, array) {
  // returns the new value instead of item

})

item      -   The value of the current element and its mandatory.
index    -   The array index of the current element and its an optional parameter.

array    -   The array object the current element belongs to the array and its an optional.


let lengths = ["One", "Two", "Three"];
var lengthTest = lengths.map(item => item.length)
alert(lengthTest); // 3,3,5






Wednesday, April 4, 2018

Array search - Filter

We know find method looks for a single (first) element. But filter will check all passed data and return an array.

The syntax is same as find method,

let results = arr.filter(function(item, index, array) {
  ..................
});

Example:


let usersArrOfObject = [
  {id: 1, name: "John"},
  {id: 2, name: "Pete"},
  {id: 3, name: "Mary"},
  {id: 4, name: "Maths"},
  {id: 5, name: "Mercy"}
];

let outputUsers = usersArrOfObject.filter(item => item.id < 3);

alert(outputUsers.length); // 2
console.log(outputUsers);

From the above we are passing an item and checking the condition which specifies id is less than 3. So passed data as returned as array.







Array search - find and find Index for an object

Just think, You have an array of objects. How do you find an object with the specific condition in that array?

Here, the arr.find method will do the job for you!

Syntax:

let result = arr.find(function(item, index, array) {
  ..........................
});

item - It is the element.
index  - It is its index.
array  It is the array itself.


Example :


let arr = [

    { name:"string 1", value:"this", other: "that" },

    { name:"string 2", value:"this", other: "that" }

];

let obj = arr.find(o => o.name === 'string 1');

console.log(obj);

Output:
{
  "name": "string 1",
  "value": "this",
  "other": "that"
}

If found happen , returns true and the search will be stopped, the item is returned. If nothing found, undefined is returned.

From the above I have used ES6 arrow function, arr having array of objects. Here o is the parameter (current item) and its getting name from the current and checking with our condition. If its will get match, returns true and further search will be stopped.

If nothing matching undefined will be returned.

Note:
We did not use any other parameter except item , mentioned in the syntax.


arr.findIndex 

               The arr.findIndex method is also same, but it will returns the index where the element was found instead of the element.










Array search elements

Actually lot of methods there for searching in an array.we can see those all later on next chapter.

Here we are going to see the below three methods.

      1. indexOf

      2. lastIndexOf 

      3. includes

Generally indexOf function means, you would have thought its working based on characters. But java script, here not the case. Its an item in java script.

A short intro about these all function. Let see...

Syntax:
arr.indexOf(item , from)

It returns the position of the first occurrence of a specified value in a string. If unable to find will return -1.

item - Its specifies what you are searching...
from - Its Optional. Default 0. At which position to start the search(item).


Syntax:
arr.lastIndexOf(item, from)

It looks from right to left. Means, returns the position of the last occurrence of a specified value in a string. If unable to find will return -1.

item - The string(item) what you are going to search.
from - Its Optional. The position where to start the search.

Syntax:
arr.includes(item, from)

It checks whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not able to find.

item - The string, what you are going to search.
from - Its an optional. Default is 0. At which position to start the search(item)

Example for indexOf:
Q: Find the first occurrence e here.


var str = "Hello world, welcome to JS";
var n = str.indexOf("e");

alert(n); // 1


Q: Search e which should start position from 5.

var str = "Hello world, welcome to javascript";
var n = str.indexOf("e", 5);

alert(n); // 14


Example for lastIndexOf:


Q: Search last occurrence  "love" from the given string.


var str = "Hello I love JS much.";
var n = str.lastIndexOf("love");
alert(n) // 8

Q: Search item "love" which should start position at 30.


var str = "Hello I love JS very much very much very much";
var find = str.lastIndexOf("very", 30);
alert(find) // 26

Example for includes:


Q: Find 'world' from below given string.


var str = "Hello world, welcome JS.";
var n1 = str.includes("world");

alert(n1); // True

Q: Find 'world' from below given string.


var str = "Hello world, welcome JS.";
var n2 = str.includes("world", 12);

alert(n2); // false