Part 3: What’s New with JavaScript in MarkLogic 10?
Posted by Dermot Smyth on 08 February 2020 05:17 PM |
|
Rest and Spread Properties in MarkLogic 10In this last blog of the series, we’ll review over the new object rest and spread properties in MarkLogic 10. As mentioned previously, other newly introduced features of MarkLogic 10 include:
The object rest and spread properties we will be reviewing were introduced with the V8 Engine upgrade, which you can learn more about in the previous blog. Now let’s go through some examples of how this new functionality is used. Object Rest and Spread PropertiesOne extremely useful piece of functionality that was included in the upgrade was object rest and spread properties, which allow you to
To see how it works, imagine that you have a JSON object that includes first name, last name, country, and state and which looks something like: const person = { firstName: 'Sebastian', lastName: 'Markbåge', country: 'USA', state: 'CA', }; Using object rest properties, you can take the person object, extract the first name to a variable, extract the last name to a variable, and then put the rest of the JSON properties into a variable called const { firstName, lastName, ...rest } = person; console.log(firstName); // Sebastian console.log(lastName); // Markbåge console.log(rest); // { country: 'USA', state: 'CA' } What happened here is:
Inversely, there’s also something called the spread operator. Imagine that you have the variables const personCopy = { firstName, lastName, ...rest }; console.log(personCopy); // { firstName: 'Sebastian', lastName; 'Markbåge', country: 'USA', state: 'CA' } In this case, the three dots next to the DHF Use Case: Add Unused Properties to Attachments PropertyOne thing that’s really useful using the rest property syntax is the ability to add unused properties to the let instance = { firstName, lastName }; instance["$attachments"] = rest; This results in the following instance, in a format we know as the envelope pattern: { "firstName": "Sebastian", "lastName": "Markbåge", "$attachments": { "country": "USA", "state": "CA", } } As you can see, DHF Use Case: Merging Two SourcesImagine that you have two JSON objects, const source1 = { firstName: "Bilbo", lastName: "Baggins" }; const source2 = { nom: "Bilbon Sacquet", location: "The Shire", }; Using the spread operator ( const instance = {...source1, ...source2); The instance now looks like the following: { "firstName": "Bilbo", "lastName": "Baggins", "nom": "Bilbon Sacquet", "location": "The Shire" } Let’s imagine that instead we wanted to combine the two objects but not include the property // filter out theName const { nom, ...filteredSource2 } = source2; const instance = {...source1, ...filteredSource2); In the first line, we’re extracting out { "firstName": "Bilbo", "lastName": "Baggins", "location": "The Shire" } These types of operations are useful if you find yourself writing custom code in the MarkLogic Data Hub. ConclusionAnd our look at rest and spread properties wraps up our blog series What’s New with JavaScript in MarkLogic 10?. I hope you enjoyed it! If you would like to review the previous blogs in the series, please check out the links below: JavaScript Modules in MarkLogic 10 — Compare the differences between JavaScript Modules (MJS) and Server-side JavaScript (SJS) by working through some examples, and see some of the advantages of upgrading to MarkLogic 10. V8 Engine Upgrade in MarkLogic 10 — Discusses the V8 Engine upgrade and the new API’s to work with, and then dives into new useful methods. The post Part 3: What’s New with JavaScript in MarkLogic 10? appeared first on MarkLogic. | |