0

I'm new to lwc as well as javascript . I have an Apex class returning List into the Lwc. Method used in LWC:

if ( strLastName ) { 
            getAssets( { strLastName } )   
            .then(result => { 
                this.contacts = result;          
                 console.log(JSON.stringify(result, null, '\t'));
            }) 
            .catch(error => { 
                this.error = error; 
            }); 
        } else 
        this.contacts = undefined;

Returned Result in Console :

[
    {
        "category": "Database Category 2",
        "classification": "Confidential",
        "exampleData": "test",
        "fieldType": "test",
        "name": "test"
    },
    {
        "category": "Database Category 2",
        "classification": "Confidential",
        "name": "Customer Name"
    },
    {
        "category": "DatabaseData",
        "classification": "Internal",
        "name": "Postal Address"
    },
    {
        "category": "Database Category 2",
        "classification": "Confidential",
        "name": "Customer Infio"
    },
    {
        "category": "DatabaseData",
        "classification": "Confidential",
        "name": "Pin"
    }
]

I need to Parse the Returned data and pass it to : (will use for-loop) // this.data = [{ 'Id': '12345', 'Category ': 'Acme', 'Calssification': 'CD355119-A', 'name': 'Hot'];

Can someone help me in parsing this in the best way possible? Any suggestions would be appreciated. Thankyou .

1
  • 2
    It's already "parsed". All you need to do is assign the value. – sfdcfox Oct 28 '20 at 0:27
0

The returned data has already been converted from the JSON string to the equivalent JavaScript object hierarchy. In this case you get back an array of objects that have properties like "name", "category" and "classification" (amongst others).

You can iterate these contacts and examine their data like:

this.contacts.forEach(contact => {
    console.log(contact.name, contact.category, contact.classification);
}

You can find out more about JavaScript objects (and other aspects of JavaScript) in lots of places, including w3schools.com for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.