How to search in an array of objects with Javascript

4 Answers

0 votes
function searchArrayofObjects(workers, s) {
    for (let i = 0; i < workers.length; i++) {
        if (workers[i].name === s) {
            return true
        }
    }
    return false;
}
 
var workers = [
    { id: 13451, name: 'Axel'},
    { id: 87134, name: 'Blaze'},
    { id: 79372, name: 'Dexter'},
    { id: 39810, name: 'Isla'}
];
   
console.log(searchArrayofObjects(workers, 'Isla'));
 
 
   
   
/*
run:
       
true
    
*/

 



answered Jun 17, 2020 by avibootz
edited Jun 17, 2020 by avibootz
0 votes
var workers = [
    { id: 13451, name: 'Axel'},
    { id: 87134, name: 'Blaze'},
    { id: 79372, name: 'Alma'},
    { id: 39810, name: 'Isla'}
];
  
  
var __SEARCH = workers.find(function(workers, index) {
	if (workers.name === 'Alma')
		return true;
});  
  
console.log(__SEARCH);


  
  
/*
run:
      
{ id: 79372, name: 'Alma' }

*/

 



answered Jun 17, 2020 by avibootz
0 votes
var workers = [
    { id: 13451, name: 'Axel'},
    { id: 87134, name: 'Blaze'},
    { id: 79372, name: 'Alma'},
    { id: 39810, name: 'Isla'}
];
   

s = 'Alma';
   
var __SEARCH = workers.find(function(workers, index) {
    if (workers.name === s)
        return true;
});  
   
console.log(__SEARCH);
 
 
   
   
/*
run:
       
{ id: 79372, name: 'Alma' }
 
*/

 



answered Jun 17, 2020 by avibootz
0 votes
var workers = [
    { id: 13451, name: 'Axel'},
    { id: 87134, name: 'Blaze'},
    { id: 79372, name: 'Alma'},
    { id: 39810, name: 'Isla'}
];
   

const result = workers.find(({ name }) => name === 'Isla');

console.log(result) 
 
 
   
   
/*
run:
       
{ id: 39810, name: 'Isla' }
 
*/

 



answered Jun 17, 2020 by avibootz

Related questions

...