0

I have a json as like this.

[{"id":"1","date":"2020-02-21","status":"present","studentid":"1"},{"id":"2","date":"2020-02-24","status":"present","studentid":"1"}]

How to store date in a variable using angular which is being subscribe d as http request.I am not able to store .This is what i tried.

 date:string=[];
 sendPostRequest()  {

  var options = { headers: new HttpHeaders({ 'Content-Type': 'text/plain' }) };
   var count=0;
  this.http.post("http://localhost/android/Api.php?apicall=getattendance", this.postData,options).
  pipe(map(res =>  res.results || []))

  .subscribe(data => {
   this.date=data['date'];


  });

please help me.I am new to angular.

midhunsivarajan
  • 49
  • 1
  • 11

7 Answers7

2

Loop through this json and put it into a new array.

const json = [{ "id": "1", "date": "2020-02-21", "status": "present", "studentid": "1" }, { "id": "2", "date": "2020-02-24", "status": "present", "studentid": "1" }];
const date = [];
json.forEach((item) => {
    date.push({
        date: item.date
    })
})
console.log(date);
wsc
  • 916
  • 7
  • 10
  • this json i am getting while subscribing.Can you please help me by telling how to store the data which is being accepted by subscription – midhunsivarajan Feb 24 '20 at 05:20
  • This problem is related to the deep and shallow copy of the object. You can learn about this information – wsc Feb 24 '20 at 05:26
2

You are getting response as an array. Convert your response to date. Please refer below code where you will get the date from your response.

date:string=[];
 sendPostRequest()  {

  var options = { headers: new HttpHeaders({ 'Content-Type': 'text/plain' }) };
   var count=0;
  this.http.post("http://localhost/android/Api.php?apicall=getattendance", this.postData,options).
  pipe(map(res =>  res.results || []))

  .subscribe(data => {
   this.date=data.map(ele => ele.date);


  });

Example,

const data = [{"id":"1","date":"2020-02-21","status":"present","studentid":"1"},{"id":"2","date":"2020-02-24","status":"present","studentid":"1"}];

const date = data.map(ele => ele.date);

console.log(date);
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
0

your date in string format and it's a single value and declare your date variable as Array [] you just need to declare your date as string variable

date : string =''; or date = '';

0

I think your question is similar to these already asked questions. If you want to store it in a variable you can look at this one: Store data from API in variable [Angular]

If you want to convert it in date format have a look at it: how to convert current date to YYYY-MM-DD format with angular 2

UPDATE

I think the only problem is not iterating properly. Something like this would be okay.

Iterate over your response and push the element to your list

.subscribe(data => {
  data.forEach((item) => {
      date.push({
          date: item.date
      })
  })
})
Maaz Ahmed
  • 53
  • 1
  • 6
0

Understand the structure of your JSON well at first. For that you can use online JSON parser available for free:

https://codebeautify.org/jsonviewer

https://jsonformatter.org/

http://json.parser.online.fr/

If you see you data after structure it is;

[
  {
    "id": "1",
    "date": "2020-02-21",
    "status": "present",
    "studentid": "1"
  },
  {
    "id": "2",
    "date": "2020-02-24",
    "status": "present",
    "studentid": "1"
  }

]

There is an array of object. So basically, you have too loop through the array visit every index of array and then pick the key-value pair that you want.

@wsc answer pointed the code well.

Apurva Pathak
  • 692
  • 1
  • 9
  • 22
0

Your response is an array of objects. Define your date variable as data:string='';

and try this.date=data[0].date

if you get any error please update.

0
    date:string=[];
 sendPostRequest()  {

  var options = { headers: new HttpHeaders({ 'Content-Type': 'text/plain' }) };
   var count=0;
  this.http.post("http://localhost/android/Api.php?apicall=getattendance", this.postData,options).
  pipe(map(res =>  res.results || []))

  .subscribe(data => {
  data.forEach(element=>{
this.date.push(element.date);
})

});