Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - Iterating an Object using .map to display the inner array values

I have a JSON stringified object as:

{
  "lesseeName": "Padyster-7",
  "lesseeRegNo": "12345",
  "lesseeVatNo": "4456",
  "telFaxNo": "1234567891",
  "billingAddress": {
    "addressId": null,
    "addressLine1": "XYz , l1 street",
    "addressLine2": "near xyz bank",
    "postalCode": "60000",
    "countryName": "MY",
    "cityName": "Kuala lumpur",
    "stateProvinceCode": "Kuala lumpur"
  },
  "mlaList": [{
    "mlaNo": 92,
    "lesseeId": 108,
    "executionDate": "27/01/2017",
    "signatoryInfo": "Test",
    "overdueRate": 3.44,
    "nonPaymentDays": 2,
    "consolidationTerm": "Monthly",
    "createdBy": null,
    "createdDtm": null,
    "updatedBy": null,
    "updatedDtm": null,
    "statusIndicator": null,
    "signatoryEmail": "[email protected]",
    "leaseMlaNo": "OPM1",
    "statusDescription": "APPROVED"
  }, {
    "mlaNo": 93,
    "lesseeId": 108,
    "executionDate": "03/01/2017",
    "signatoryInfo": "tess",
    "overdueRate": 5.77,
    "nonPaymentDays": 2,
    "consolidationTerm": "Bi-Monthly",
    "createdBy": null,
    "createdDtm": null,
    "updatedBy": null,
    "updatedDtm": null,
    "statusIndicator": null,
    "signatoryEmail": "[email protected]",
    "leaseMlaNo": "OPM2",
    "statusDescription": "APPROVED"
  }]
}

I am working in Reactjs and I want my object to be iterated such that the inner array mlaList of objects gets iterated to display value one after other. whenever I try using the .map function to the parent object I get an error saying ".map is not a function" below is the iteration I attempt which fails:

{data.map((data, index) => {data.leaseMlaNo}   {data.signatoryEmail})}

I have referred to the SO questions quite similar to this one, but they just talk about iterating the objects using Object.keys

Please help me understand what I am doing wrong and what should be the correct way to achieve this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The method Array#map is a method of the Array class, and not of the Object class. However, the mlaList property is an array, and you can iterate it. You should use data.mlaList.map():

// if the data is stringified - const data = JSON.parse({ the object });
const data = {"lesseeName":"Padyster-7","lesseeRegNo":"12345","lesseeVatNo":"4456","telFaxNo":"1234567891","billingAddress":{"addressId":null,"addressLine1":"XYz , l1 street","addressLine2":"near xyz bank","postalCode":"60000","countryName":"MY","cityName":"Kuala lumpur","stateProvinceCode":"Kuala lumpur"},"mlaList":[{"mlaNo":92,"lesseeId":108,"executionDate":"27/01/2017","signatoryInfo":"Test","overdueRate":3.44,"nonPaymentDays":2,"consolidationTerm":"Monthly","createdBy":null,"createdDtm":null,"updatedBy":null,"updatedDtm":null,"statusIndicator":null,"signatoryEmail":"[email protected]","leaseMlaNo":"OPM1","statusDescription":"APPROVED"},{"mlaNo":93,"lesseeId":108,"executionDate":"03/01/2017","signatoryInfo":"tess","overdueRate":5.77,"nonPaymentDays":2,"consolidationTerm":"Bi-Monthly","createdBy":null,"createdDtm":null,"updatedBy":null,"updatedDtm":null,"statusIndicator":null,"signatoryEmail":"[email protected]","leaseMlaNo":"OPM2","statusDescription":"APPROVED"}]};

const result = data.mlaList.map((o, index) => o.signatoryEmail); // in react <div key={index}>{o.signatoryEmail}</div> for example

console.log(result);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...