> For the complete documentation index, see [llms.txt](https://readme.directual.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://readme.directual.com/javascript-sdk/sdk-examples.md).

# JS SDK: Examples

## Examples of Array Handling

### Example 1: **Finding Intersection of Arrays**

In this example, we will match candidates (structured as 'Cand') with job offers (structured as 'Jobs') by considering the intersection of skills, represented as arrays such as finance, management, and programming. The candidate's skills are stored in 'cand\_skills,' while the required skills (functions) for the job are stored in 'job\_functions.' The matching result will be saved in the 'Job' object, specifically in the 'candidate\_ids' field.

Here we use the following functions:

* [$D.getValueFromTemplate()](/javascript-sdk/internal-usdd-methods.md#get-field-value-of-the-current-object-usdd-getvaluefromtemplate)
* Standard JS method [.split(",")](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
* Standard JS method [.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
* [$D.store.get()](/javascript-sdk/internal-usdd-methods.md#get-an-object-from-different-structure-usdd-store-get)
* [$D.arrayContainsAny()](/javascript-sdk/internal-usdd-methods.md)
* [$D.concat()](/javascript-sdk/internal-usdd-methods.md)
* [$D.store.save()](/javascript-sdk/internal-usdd-methods.md#save-an-object-usdd-store-save)

Content within the [JS SDK Step](/scenarios/editing-scenarios/action-steps/js-sdk-step.md):

```javascript
// Scenario processes objects from Jobs

DirectualEngine.addEventListener(AppEvents.START, function(context){
  var candidates = $D.eval("{{candidates_ids}}")
  var functions = $D.eval("{{job_functions}}")
  var id = $D.eval("{{id}}")

  var matchCand = []
  
  if (candidates) {
  	candidates.split(",").forEach(function(cand) {
    	var candSkills = $D.store.get("Cand", cand, {fields: ["cand_skills"]});
       	if ($D.arrayContainsAny(candSkills, functions)) {
        	matchCand = $D.concat(matchCand,cand)
        }
    })
  }
  $D.store.save("Jobs", {"id": id, "candidate_ids": matchCand }, false)
  return true;
});
```

### Example 2: Composing JSON with Custom Property Names

While the [JSON step](/scenarios/editing-scenarios/action-steps/json-step.md) is useful, it doesn't support custom property names. Here's how to work around that limitation.

For instance, if you have structures for 'Authors' and 'Books' and want to compose JSON like this:

```javascript
{
    "name": "Leo Tolstoy",
    "book1": {
        "id": "book1",
        "title": "War and Peace"
    },
    "book2": {
         "id": "book2",
         "title": "Anna Karenina"
     }
 }
```

We can do that with the following JS SDK code:

```javascript
DirectualEngine.addEventListener(AppEvents.START, function(context){
  var id = $D.eval('{{id}}')
  var array = $D.eval('{{book_ids}}').split(',')
  var newObject = {}
  newObject.name = $D.eval('{{name}}')
  array && array.map(function(book) {
    newObject[book] = {}
    var bookObject = $D.store.get("Books", book, {fields: ["Title", "Year"]});
    for (var field in bookObject) {
    	newObject[book][field] = bookObject[field]
    }
  })
  //saving result object
  $D.store.save("WebUser", {"id": id, "json": JSON.stringify(newObject) }, false)
  return true
});
```
