kinnu
kinnu

Reputation: 388

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?

Upvotes: 1

Views: 3854

Answers (7)

cнŝdk
cнŝdk

Reputation: 32145

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);

Upvotes: 1

Nitesh Shaw
Nitesh Shaw

Reputation: 216

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

Upvotes: 0

waleed
waleed

Reputation: 464

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);

Upvotes: 2

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

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);

Upvotes: 0

kkangil
kkangil

Reputation: 1746

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)

Upvotes: 0

Krantisinh
Krantisinh

Reputation: 1729

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");

Upvotes: 0

Chriss Hd
Chriss Hd

Reputation: 643

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";
}

Upvotes: 1

Related Questions