Skip to main content

How to use some function in javascript? Best 5 example of some function in js.


JavaScript Array some() Method
JavaScript some Function - 
Some Function :

The basic use of this function is to get details of record from Array according to some properties. The some function is very helpful with JSON Array. the return type of this function is boolean , either true or false

Syntax:arrayname.some((user define variable name)=>{return condition});

Lets understand this with basic examples :

Q1. how to get all employees with gender male ?
    
 var employee_list=[
 {'id':'1','name':'Mukund Singh','gender':'M'},
{'id':'2','name':'Arun','gender':'M'},
{'id':'3','name':'Chanu','gender':'F'},
{'id':'4','name':'Main','gender':F'}
                                    ]
console.log(employee_list.some((v)=>{return v.gender=='M'}));
Here you will result :
true

Q2. how to get all products with active status  true ?

var product_list=[
 {'id':'1','name':'Apple','is_active':false},
{'id':'2','name':'Pizza','is_active':false},
{'id':'3','name':'Milk','is_active':false},
{'id':'4','name':'Orange','is_active':false}
                                    ]
console.log(product_list.some((v)=>{return v.is_active}));
Here you will result :

false

Q3. how to get all products with MRP more than 100 ?

var product_list=[
 {'id':'1','name':'Apple','mrp':20},
{'id':'2','name':'Pizza','mrp':11},
{'id':'3','name':'Milk','mrp':20},
{'id':'4','name':'Orange','mrp':-40}
                                    ]
console.log(product_list.some((v)=>{return v.mrp>100}));
Here you will result :

false

Q4. how to get all products with MRP more than 100 and active status true?

var product_list=[
 {'id':'1','name':'Apple','mrp':20,'is_active':true},
{'id':'2','name':'Pizza','mrp':1001,'is_active':true},
{'id':'3','name':'Milk','mrp':200,'is_active':false},
{'id':'4','name':'Orange','mrp':-40,'is_active':true}
                                    ]
console.log(product_list.some((v)=>{return v.mrp>100 && v.is_active}));
Here you will result :

true

Q5. how to get all products with MRP more than 100 or active status true?

var product_list=[
 {'id':'1','name':'Apple','mrp':20,'is_active':false},
{'id':'2','name':'Pizza','mrp':1001,'is_active':true},
{'id':'3','name':'Milk','mrp':200,'is_active':false},
{'id':'4','name':'Orange','mrp':-40,'is_active':true}
                                    ]
console.log(product_list.some((v)=>{return v.mrp>100 || v.is_active}));
Here you will result :

true
for any doubt write comment

Comments

Popular posts from this blog

How to read or import excel file in angular 2,4,5,7,8,10 Hindi/English

How to Upload Files with cPanel File Manager in Hostinger web hosting in...

How to use every function in javascript? Best 5 example of every function in js.

JavaScript every Function -  Every Function :