Progress Acquires MarkLogic! Learn More

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

Back to blog
4 minute read
Back to blog
4 minute read
City at night with illustration of a global network

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

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!)

Read more by this author

Share this article

Read More

Related Posts

Like what you just read, here are a few more articles for you to check out or you can visit our blog overview page to see more.

Developer Insights

Multi-Model Search using Semantics and Optic API

The MarkLogic Optic API makes your searches smarter by incorporating semantic information about the world around you and this tutorial shows you just how to do it.

All Blog Articles
Developer Insights

Create Custom Steps Without Writing Code with Pipes

Are you someone who’s more comfortable working in Graphical User Interface (GUI) than writing code? Do you want to have a visual representation of your data transformation pipelines? What if there was a way to empower users to visually enrich content and drive data pipelines without writing code? With the community tool Pipes for MarkLogic […]

All Blog Articles
Developer Insights

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

V8 Engine Upgrade in MarkLogic 10 In this continuation of the series, What’s New with JavaScript in MarkLogic 10?, we’ll discuss in detail about the new API’s the V8 Engine upgrade comes with. In the previous blog, we mentioned how MarkLogic 10 comes with a ton of new features, some of which include: The addition […]

All Blog Articles

Sign up for a Demo

Don’t waste time stitching together components. MarkLogic combines the power of a multi-model database, search, and semantic AI technology in a single platform with mastering, metadata management, government-grade security and more.

Request a Demo