1

I need help! According to this post (https://developer.salesforce.com/blogs/2019/11/content-delivery-api-to-extend-or-integrate-content.html)I managed to receive content from our CMS.

Now I would like to sort the received in the JS controller, I therefore created a contentNode "sortnumber".

This is the JS adapter - any ideas?

import { LightningElement, wire } from "lwc";
import initMethod from "@salesforce/apex/MCWrapperController.initMethod";
export default class tripItinerary extends LightningElement {
  @wire(initMethod) results;
  
  /* my custom function, will use it later */
  get jsonData() {
    console.log('results', this.results.data); //is working fine I get all the data
    // test 1: failed
    //const result = JSON.parse(JSON.stringify(this.results));
    //result.data.sort();
    // test 2: failed
    // const result = this.results.data;
    // result.sort(function (a, b) {
    //   return a.data.contentNodes.sortnumber.value - b.data.contentNodes.sortnumber.value;
    // });
    return this.results;
    }
}

The problem is, that I get no access to a sortable array!!

Thank you in advance!!

1
  • What does your data actually look like?
    – sfdcfox
    Commented Mar 12, 2021 at 12:34

1 Answer 1

3

You can do it like this I guess

import { LightningElement, wire } from "lwc";
import initMethod from "@salesforce/apex/MCWrapperController.initMethod";
export default class tripItinerary extends LightningElement {
  results
  @wire(initMethod)
  getResults(response) {
    const {data, error} = response;
    if (data) {
      // if your structure is different, to debug your actual data
      // console.log(data);
      this.results = [...data].sort((a, b) => a.data.contentNodes.sortnumber.value - b.data.contentNodes.sortnumber.value)
    } else if (error) {
      // handle error
    }
  }
}
12
  • You need to shallow copy, but you don't need to deep copy (stringify/parse). You also don' need return if you don't use {}. It could be written this.results = [...data].sort((a, b) => a.data.contentNodes.sortnumber.value - b.data.contentNodes.sortnumber.value);
    – sfdcfox
    Commented Mar 12, 2021 at 13:55
  • I added JSON.parse(JSON.stringify(data)) just for debugging purposes, but you're right, just after coming from Apex data is not yet wrapped into Proxy
    – ytiq
    Commented Mar 12, 2021 at 14:15
  • regarding sort, just wanted to leave the code untouched if possible, so it's easier to understand. Don't think [...data] here is necessary (shallow copy) either
    – ytiq
    Commented Mar 12, 2021 at 14:17
  • 3
    Array.sort modifies the original array, but won't be able to because the data will be wrapped in a read-only membrane. The read-only membrane protects the internal cache that could otherwise be corrupted. As such, the spread operator [...value] is necessary here.
    – sfdcfox
    Commented Mar 12, 2021 at 14:23
  • 1
    @Maserick someArray.slice() and [...someArray] do the same thing.
    – sfdcfox
    Commented Mar 14, 2021 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .