We’ve joined forces with Smartlogic to reveal smarter decisions—together.

Part 3: What’s New with JavaScript in MarkLogic 10?

Rest and Spread Properties in MarkLogic 10

In 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:

  1. The addition of JavaScript Modules, also known as MJS (discussed in detail in the first blog in this series).
  2. The V8 Engine was upgraded from version 5.3 to version 6.7. This is the actual motor that ‘powers’ JavaScript in MarkLogic.
  3. Additional features from the ECMAScript 2015 JavaScript standard have been added. Go to http://www.ecma-international.org/ecma-262/6.0/ to find out what you can do with ECMAScript 2015.

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 Properties

One extremely useful piece of functionality that was included in the upgrade was object rest and spread properties, which allow you to

  1. Easily split up a JSON object into multiple objects, or
  2. Combine multiple JSON objects into a single object.

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 otherProps.

const { firstName, lastName, ...otherProps } = person; 
console.log(firstName); // Sebastian
console.log(lastName); // Markbåge
console.log(otherProps); // { country: 'USA', state: 'CA' }

What happened here is:

  • the value of the property firstName was assigned to a variable called firstName
  • the value of the property lastName was assigned to a variable called lastName
  • and the three dots next to the otherProps property means create a JSON object containing all the rest of the properties and their values and assign it to the otherProps

Inversely, there’s also something called the spread operator. Imagine that you have the variables firstName and lastName, along with a JSON object otherProps. We can combine these into a single JSON object using the spread operator as follows:

const personCopy = { firstName, lastName, ...otherProps }; 

// { firstName: 'Sebastian', lastName; 'Markbåge', country: 'USA', state: 'CA' }

In this case, the three dots next to the otherProps property means expand (or ‘spread’) out the properties in the JSON object for easy assignment in the new object.

DHF Use Case: Add Unused Properties to Attachments Property

One thing that’s really useful using the rest property syntax is the ability to add unused properties to the $attachments property. Continuing with our person example, what we can do is create an instance object which contains the first and last name, with the rest of the properties in ” $attachments“:

let instance = { firstName, lastName }; 

instance["$attachments"] = otherProps;

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, $attachments contains the rest of the properties that were not used. This way we keep all our original data in a manageable way.

DHF Use Case: Merging Two Sources

Imagine that you have two JSON objects, source1 and source2:

const source1 = {
  firstName: "Bilbo",
  lastName: "Baggins"

const source2 = {
  nom: "Bilbon Sacquet",
  location: "The Shire",

Using the spread operator (...) we learned about earlier, we can merge these two sources quite easily using the following code:

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 nom. We could adjust our code to the following:

// filter out theName

const { nom, ...filteredSource2 } = source2;

const instance = {...source1, ...filteredSource2);

In the first line, we’re extracting out nom and putting all of the other properties into a new variable filteredSource2. Then, in the second line of code, we combine these properties with the original source1, to end up with the following:

 "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.


And 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.

Dermot Smyth - Senior Consultant | MarkLogic

Dermot is a Senior Technologist with over 15 years of experience architecting, developing and delivering software using state-of-the-art technologies. Prior to joining MarkLogic, he worked internationally in a broad range of industries including Investment Banking, Insurance, Energy and Tertiary Education.

Originally from Australia, he now lives in Paris and is enjoying working his way through all the different types of French cheese (over 400!)

Start a discussion

Connect with the community




Most Recent

View All

Unifying Data, Metadata, and Meaning

We're all drowning in data. Keeping up with our data - and our understanding of it - requires using tools in new ways to unify data, metadata, and meaning.
Read Article

How to Achieve Data Agility

Successfully responding to changes in the business landscape requires data agility. Learn what visionary organizations have done, and how you can start your journey.
Read Article

Scaling Memory in MarkLogic Server

This not-too-technical article covers a number of questions about MarkLogic Server and its use of memory. Learn more about how MarkLogic uses memory, why you might need more memory, when you need more memory, and how you can add more memory.
Read Article
This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.