Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find vs filter

const requests = [{  
    App: 'Adobe',  
    Count: 10  
}, {  
    App: 'Apple',  
    Count: 12  
}, {  
    App: 'Amazon',  
    Count: 5  
}, {  
    App: 'Microsoft',  
    Count: 2  
}];


requests.find(item => item.App == "Apple")
// output: {App: "Apple", Count: 12}
// find method returns actual value


requests.filter(item => item.App == "Apple")
//output: [ {App: "Apple", Count: 12} ]
// find method returns array of actual values
Comment

javascript find vs filter

both usually work: the format is identical
find gets the first value that matches your criteria
filter gets all the values in the array that matches your criteria

Example

  let dates = ["2022-10-17", "2022-10-18"];

  for(var i in dates)
   { customDatesStyles.push({
        startDate: moment(dates[i])
      });

    }
function change(da)
 { console.log(customDatesStyles[0].startDate);
  console.log(da);
let d = customDatesStyles.find((el)=> el.startDate.format("YYYY-MM-DD") == da.format("YYYY-MM-DD")));

 }
both filter and find would have been fine.
The answer with filter would just have needed to be d[0] (although there is only one in this case).
But filter can have more than one case, whereas find stops at the first found case.
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to turn of autocomplete in react hook form material ui 
Javascript :: mutation observer 
Javascript :: how to clear textbox in javascript 
Javascript :: expo app.json 
Javascript :: validate country wise phone code javascript 
Javascript :: break loop if condition is met 
Javascript :: javascript filter array 
Javascript :: javascript reducer 
Javascript :: how to set three js canvas width 100% 
Javascript :: getattribute 
Javascript :: sequelize migration limit 
Javascript :: could not find react-redux context value; please ensure the component is wrapped in a <Provider 
Javascript :: make indexOF in js 
Javascript :: nextjs amp 
Javascript :: import in react 
Javascript :: append array in array 
Javascript :: how can i use exact in react router dom v6 
Javascript :: switch statement js 
Javascript :: try catch throwing error in javascript 
Javascript :: how to add class in jquery 
Javascript :: populate in mongoose 
Javascript :: stripe payment js 
Javascript :: JavaScript is case-sensitive 
Javascript :: javascript find textarea 
Javascript :: pure component in react 
Javascript :: ternary operator shorthand javascript 
Javascript :: javascript block link action 
Javascript :: javascript last elements same class 
Javascript :: js let vs var performance 
Javascript :: sort datatable c# 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =