Mapping the key value in angularjs

Viewed 33

I have array of object. where i have to loop and get information of text and create key value pair >i tried below code some how its getting added as header but value is not populating.I need header like note1... to note25..

Sample response ::

registrationD:{
 notes: [
        {
          id: "13,
          text: "",
          lastModified: "20Z",
        },
        {
          id: "4d4",
          text: "",
          lastModified: "2022-08-25T09:14:20Z",
        },
        {
          id: "89",
          text: "",
          lastModified: "2022-08-:20Z",
        },
      ],
}
`
for (let i = 0; i < 25; i++) {
      this.notesColumns.push({
        field: registrationD.notes[ i ]?.text ,
        header: `note${
          i + 1
        }`
      });
    }
1 Answers

I put in the response provided in the question within an object.

const obj = {registrationD:{
 notes: [
        {
          id: "13",
          text: "",
          lastModified: "20Z",
        },
        {
          id: "4d4",
          text: "",
          lastModified: "2022-08-25T09:14:20Z",
        },
        {
          id: "89",
          text: "",
          lastModified: "2022-08-:20Z",
        },
      ],
}}

Make sure notesColumns is defined as an array. If you are only using it in the method, better to use const/let, and then return it to the caller.

this.notesColumns = [];

or

const notesColumns = [];

Then you can run your code as:

for (let i = 0; i < 25; i++) {
      this.notesColumns.push({
        field: obj.registrationD.notes[i]?.text ,
        header: `note${
          i + 1
        }`
      });
}

(use only notesColumns without this if using const)

Hope this helps.

Related