简体   繁体   中英

Array.filter is returning empty array

I am trying to get the failure Details based on Date. For this I am applying array.filter but it is returning empty array.

Below is my array:

value:[{
  "Date": "02/04/2019",
  "Total": "1000",
  "Success": "850",
  "Failure": "150",
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}, {
  "Date": "03/04/2019",
  "Total": "800",
  "Success": "750",
  "Failure": "150",
  "FailureDeatils": [{
    "Reason": "Reason1",
    "Count": 3
  }, {
    "Reason": "Reason2",
    "Count": 1
  }]
}]      

And if I give the date as 02/04/2019,it should return the following:

{
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}

I am using below array.filter method:

var filtered = value.filter(isPresent);
function isPresent(value) {
  return value == 02/04/2019;
}

this is returning empty array.

Can anyone please figure me out where I am going wrong?

In your function isPresent , value is each object in the array

And you're compaing the entire object to a value

What you need to do is compare the object property of that object

function isPresent(value) {
    return value.Date == "02/04/2019";
}

You need to check over value.Date and not value because value or the callback param will hold the whole iterated object from your array :

function isPresent(value) {
  return value.Date && value.Date == "02/04/2019";
}

To get only FailureDeatils from your filtered object , you can map the filtered result with Array#map() method :

var filtered = data.filter(isPresent).map(o => { return {"FailureDeatils": o.FailureDeatils}});

Note:

  • Make sure to wrap "02/04/2019" between "" so it can be evaluated as string and correctly compared, otherwise it will be calculated and treated as Number and gives wrong filtering results.
  • Avoid using the same variable name value twice for your array and for the callback parameter for better readability of your code.

Demo:

 var data = [{ "Date": "02/04/2019", "Total": "1000", "Success": "850", "Failure": "150", "FailureDeatils": [{ "Reason": "Reason1", "Count": 2 }, { "Reason": "Reason2", "Count": 6 } ] }, { "Date": "03/04/2019", "Total": "800", "Success": "750", "Failure": "150", "FailureDeatils": [{ "Reason": "Reason1", "Count": 3 }, { "Reason": "Reason2", "Count": 1 }] }] var filtered = data.filter(isPresent).map(e => { return {"FailureDeatils": e.FailureDeatils}}); function isPresent(value) { return value.Date && value.Date == "02/04/2019"; } console.log(filtered); 

Here is the working code based on your Array.

var value = [{
  "Date": "02/04/2019",
  "Total": "1000",
  "Success": "850",
  "Failure": "150",
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}, {
  "Date": "03/04/2019",
  "Total": "800",
  "Success": "750",
  "Failure": "150",
  "FailureDeatils": [{
    "Reason": "Reason1",
    "Count": 3
  }, {
    "Reason": "Reason2",
    "Count": 1
  }]
}];

var filtered = value.filter (isPresent).map (obj => obj.FailureDeatils);;
function isPresent (value) {
  return value.Date == "02/04/2019";
}

console.log (filtered);
var filtered = value.filter(function(a){
                   return a.Date === "02/04/2019";
               });

You should do like this -

const value = [{
  "Date": "02/04/2019",
  "Total": "1000",
  "Success": "850",
  "Failure": "150",
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}, {
  "Date": "03/04/2019",
  "Total": "800",
  "Success": "750",
  "Failure": "150",
  "FailureDeatils": [{
    "Reason": "Reason1",
    "Count": 3
  }, {
    "Reason": "Reason2",
    "Count": 1
  }]
}]

const filtered = value.filter((x) => x.Date === "02/04/2019");

 const value = [{ "Date": "02/04/2019", "Total": "1000", "Success": "850", "Failure": "150", "FailureDeatils": [{ "Reason": "Reason1", "Count": 2 }, { "Reason": "Reason2", "Count": 6 } ] }, { "Date": "03/04/2019", "Total": "800", "Success": "750", "Failure": "150", "FailureDeatils": [{ "Reason": "Reason1", "Count": 3 }, { "Reason": "Reason2", "Count": 1 }] }] var filtered = value.filter(isPresent).map(row => { return { FailureDeatils : row.FailureDeatils } }); function isPresent(value) { return value.Date === "02/04/2019"; } console.log(filtered) 

You can do:

 const value = [{"Date": "02/04/2019","Total": "1000","Success": "850","Failure": "150","FailureDeatils":[{"Reason":"Reason1","Count":2}, {"Reason":"Reason2","Count":6}]},{"Date": "03/04/2019","Total": "800","Success": "750","Failure": "150","FailureDeatils": [{"Reason":"Reason1","Count":3},{"Reason":"Reason2","Count":1}]}]; const isPresent = ({Date}) => Date === '02/04/2019'; const filtered = value.filter(isPresent); console.log(filtered); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM