-2

Please find the below data. Here is I need only Id:1 data. Some Id's have no values and some Id's have another values.

Actual data:

data: [
    0: {Id: "1", DoctorId: "61", CreatedBy: "1", CreatedDate: "2020-06-25T00:00:00.000Z",…}
    1: {Id: "2", DoctorId: "61", CreatedBy: "1", CreatedDate: "2020-06-26T00:00:00.000Z",…}
    2: {Id: "3", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-24T00:00:00.000Z",…}
    3: {Id: "1", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-24T19:24:00.000Z",…}
    4: {Id: "1", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-25T11:15:00.000Z",…}
    5: {Id: "", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-07-07T12:15:00.000Z",…}
    6: {Id: "", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-25T11:15:00.000Z",…}
    7: {Id: "", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-07-07T12:15:00.000Z",…}
    ]

I need output like this.

data: [
    0: {Id: "1", DoctorId: "61", CreatedBy: "1", CreatedDate: "2020-06-25T00:00:00.000Z",…}
    1: {Id: "1", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-24T19:24:00.000Z",…}
    2: {Id: "1", DoctorId: "61", CreatedBy: null, CreatedDate: "2020-06-25T11:15:00.000Z",…}
    ]
halfer
  • 19,824
  • 17
  • 99
  • 186
vijay
  • 15
  • 3

2 Answers2

0

Simply use Array.filter to filter the required data.

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

For example -

const modifiedData = data.filter(item => item.id === 1);
console.log(modifiedData);
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • As an user with 63k+ rep, I'd expect at least an answer with a quick demo even if hundreds of similar trivial questions have been answered already. – ruth Jul 29 '20 at 06:35
  • 1
    true that @MichaelD, but I was trying to sort OP's posted content for example, but there were a lot of mistakes in JSON so being lazy skipped that part. my bad. – Pardeep Jain Jul 29 '20 at 06:38
  • 1
    @MichaelD: it would be better if users did not answer such questions at all. It has now been rightfully closed. – halfer Jul 29 '20 at 18:48
0

Use below code.

data.filter(x=>x.id == 1);
surendra kumar
  • 1,686
  • 11
  • 15