Day 12 : Manage nested Object in your Lightning Web Components

By

3 minutes de lecture
Image d'une poupée russe de Noël

What’s new today?

Wrapper Class gives extra flexibility to Salesforce developers. A wrapper or container class is a data structure that contains different objects or collections of objects as its members. We all have used it in Visualforce pages, Lightning Aura Component and now in Lightning Web Component.

These types of objects are generally nested objects.

But, as you know, since winter 20, security for immutable @api properties has been increased for LWC. Each Object’s elements are read-only to prevent modification so that the cache can’t be corrupted by references. If you try it, you will see this error :

[LWC component’s @wire target property or method threw an error during value provisioning. Original error: 
[Cannot assign to read only property ‘Phone’ of object ‘#<Object>’]]

To fix it, you have to copy the elements using the spread operator (…), or Object.assign into a new object but you will remove the read-only properties of all top-level elements. If you want to change a property of an object in an object in an object etc… (nested object) it is not so easy and you have to copy all the nested object’s elements.

For every project I worked on, I got used to create in my LWC javascript utility lib the two functions below to simulate a custom Object.assign for nested objects.

/**********************************************
* Two functions to set a value of nested items
* key string descriptor inside an Object.
**********************************************/
const setNestedKey = (obj, path, value) => {
  if (path.length === 1) {
    let key = path[0];
    if (Array.isArray(obj)) {
      let table = [...obj];
      table.splice(key, 1, value);
      obj = [...table];
      return obj;
    }
    obj = { ...obj, [key]: value }
    return obj;
  }
  return setNestedKey(obj[path[0]], path.slice(1), value)
}

const assignObject = (obj, path, value) => {
  // Protect against being something unexpected
  obj = typeof obj === 'object' ? obj : {};
  // Split the path into and array if its not one already
  let keys = Array.isArray(path) ? path : path.split('.');
  let newObj = setNestedKey(obj, keys, value);
  if (keys.length === 1) {
    return newObj;
  }
  keys.pop();
  return assignObject(obj, keys, newObj);
}
/**********************************************/

Let me show you how to use them

For example, if you have this nested object below :

let myAccounts = [
  {
    name : 'account name 1',
    contact : [
      {
        firstName : 'first name 1',
        lastName : 'last name 1'
      },
      {
        firstName : 'first name 2',
        lastName : 'last name 2'
      }
    ]
  },
  {
    name : 'account name 2',
    contact : [
      {
        firstName : 'first name 1',
        lastName : 'last name 1'
      },
      {
        firstName : 'first name 2',
        lastName : 'last name 2'
      }
    ]
  }
];

to change the contact last name 2 of account name 2, you can use the assignObject function like this :

myAccounts = assignObject(myAccounts,'1.contact.1.lastName','new last name');

and you can also add a new contact like this :

myAccounts = assignObject(myAccounts,'1.contact',
  {
    firstName : 'first name 3',
    lastName : 'last name 3'
  }
);

I think it is very useful and hope you will like these utility functions!

You can learn more about LWC properties security here :

Join us tomorrow for more fun with Texeï’s Advent Calendar! 🎁

Want to learn more about Lightning? So, don’t miss our articles Refresh a record page from a Lightning web component and From Visualforce to Lightning Development

#Texei #SalesForce #AwesomeDeveloper

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices