0

I have a json array like this.

[
  {
    "id": "1",
    "date": "2020-02-21",
    "status": "present",
    "studentid": "1"
  },
  {
    "id": "2",
    "date": "2020-02-24",
    "status": "present",
    "studentid": "1"
  },
  {
    "id": "3",
    "date": "2020-02-25",
    "status": "absent",
    "studentid": "1"
  },
  {
    "id": "4",
    "date": "2020-02-26",
    "status": "absent",
    "studentid": "1"
  }
]

I can store the date by using this code snippet

if (data instanceof Array) {

                  this.date=data.map(ele => ele.date);
              }

How to store the dates in two arrays based on absent and present conditions.That is i want to store date_present in one array and date_absent in another array.

3 Answers 3

2

Using Map function to iterate and check the status of each element

var present = [];
    var absent = [];
    data.map(ele => {
      ele.status === "absent" ? absent.push(ele) : present.push(ele);
    });
Sign up to request clarification or add additional context in comments.

2 Comments

I have kept changedetectionStratergy as default.So do i need to change it to onPush
yes . inside the onPush method , keep this code so that the arrays are generated at each push
0

Use forEach to loop through the array and push the object to separate arrays

var date_present = [];
var date_absent = [];

data.forEach(ele => {
   if(ele.status ===  "present"){
    date_present.push(ele)
  }
   else if(ele.status ===  "absent"){
    date_absent.push(ele)
  }
});

1 Comment

I have kept changedetectionStratergy as default.So do i need to change it to onPush
0

Try like this:

var date_present = [];
var date_absent = [];

date_present = data.filter(item => item.status ===  "present")
date_absent = data.filter(item => item.status ===  "absent")

1 Comment

how can i get dates .it will store the whole content of data rigt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.