Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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.
 
PREVIOUS NEXT
Tagged: #javascript #find #filter
ADD COMMENT
Topic
Name
1+6 =