Home > Blockchain >  find is returning only the first data of a date
find is returning only the first data of a date

Time:10-28

find funtion is returning only first data row of array i want all rows in the array when logging nightbefore

 {data.map((arr) => {
   let filteredData6 = [];
          var nightbefore = arr.find((o) => o.medicationTime === "night before food" );
         nightbefore.Medicines.forEach((e) => {
          filteredData6.push(e);
          setmedicine6(filteredData6);
           });
          
            console.log(nightbefore);

arr is used to sort data by date and then i am finding all data with the medicationTime as given but only the first arry is being feched when i use find

full code i have given in codesandbox for refercence https://codesandbox.io/s/weathered-bird-7ppy7?file=/src/App.js

CodePudding user response:

Array.prototype.find is indeed only returning the first match of an array as mentioned in its documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

In order to achieve your goal you'll have to use the filter prototype:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

the code will then be :

var nightbefore = arr.filter((o) => o.medicationTime === "night before food" );
  • Related