šŸš€ RunJS 1.3.2

Okay I think you mean similar to this?
Current set up:

Output:
The acTagsArr that I set with ā€˜SetValueā€™ is getting the values pushed as expected.

 "acTagsArr": [
    "tag1",
    "tag2",
    "tag3"
  ]

Then I just need the last step: .join(',')
So I put it in the repeat now, because it should join the array after the last repeat.

Outcome:

 "repeatACtags": [
    {
      "ac_tags": [
        "BOUGHT | NTSL Ebook"
      ],
      "pushToArray": 1,
      "joinArray": "tag1"
    },
    {
      "ac_tags": [
        "these are tags",
        "here"
      ],
      "pushToArray": 3,
      "joinArray": "tag1,tag2,tag3"
    }
  ]

But then Iā€™m not sure how I can get that final joined array.
Trying like this:

But this is not giving me any output.
EDIT: Which makes sense to me now because itā€™s an array, so I need to select the last item somehow. Iā€™ll try this out now

Please note the variable scope, thereā€™s no guarantee youā€™ll be able to access the variable joinArray outside the Repeat step. You need to define a global name for that (name ā€œjoinArrayā€, global name ā€œjoinArrayGlobalā€
And then on finalArray you get {{joinArrayGlobal}} - you wonā€™t see this in the Data Binding Picker

Happy to help :slight_smile:

Oh wow that works perfectly. First time Iā€™m using the Global Name propertyā€¦

Thanks @Apple I appreciate it a lot!!

Iā€™m still wondering if I can replace my custom formatter with this extension?
Iā€™m using a pushSpread formatter which is just:

exports.pushSpread = function (arr, obj) {
    return arr.push(...obj);
}

image

return acTagsArr.push(...ac_tags)

One liner:

return [tags.map((e) => e.ac_tags).join(', ')]

Should return a single array of tags comma separated.

But acTagsArr is defined as a ā€˜set valueā€™ image . So with the custom formatter I can push things into this array. But with the runjs Iā€™m not sure how to push into this array

Today I learned :point_up:

But with the runjs Iā€™m not sure how to push into this array

Yeahā€¦ Probably would have to convert acTagsArr to a global variable, and then Set Value on it with the output of RunJS.

Here comes the king of one-liners, Mr. Jon Liner :merman:

1 Like

I love one liners. Itā€™s like completing the newspaper puzzle :joy: itā€™s a hobby

Hmm nice ideaā€¦ Need to move on now though so wonā€™t try it, but thanks for all the input. Learning a lot :slight_smile:

@JonL Very curious how youā€™d approach thisā€¦ Your one liner looks good but leaves me with 2 issues still:

  1. the join is not foolproof. Right now the tags can be separated with or without trailing spaces depending on user input. (Iā€™ll sanitize that input better in the future but Iā€™d like server side code that can handle exceptions)

  2. The global variable issue. See my earlier post: šŸš€ RunJS 1.2.0
    Any alternative idea on how to iterate over each object, grab the values of a certain key and add them to a global array?

For the whitespace just replace it with nothing.

return [tags.map((e) => e.ac_tags.replace(/\s/g, '')).join(',')]

The global variable I donā€™t understand really your issue. Just assign the output of your RunJS step to a global variable.

1 Like

@karh I have reviewed the conversation and I still donā€™t quite get the issue with the global variable.

You mentioned that your expected outcome was an array with tags in a comma separated format.

acTagsArr = ["tag1, tag2, tag3"]

The previous one liner should provide exactly that and remove any whitespace.

return [tags.map((e) => e.ac_tags.replace(/\s/g, '')).join(',')]

Now I am lost why you need the global variable? Is it for the next steps?

Hi,

This may seem a stupid question but how to I install this custom extension?
Iā€™ve downloaded the v.1.2 zip from github, and placed the 2 files in the extensions folder as per image below:

Is this correct, or do I have to activate it somehow?

Thanks

You need to remove the folder runjs as both files should live in the modules folder.

1 Like

Thanks Jon!
May be worth mentioning how to install/setup on first post with this screenshot?

1 Like

Thatā€™s been covered in the custom extensions docs already :slight_smile:

@JonL I appreciate your persistence in trying to help.

Letā€™s try figure this out :smiley: Great learning opportunityā€¦

Iā€™ve moved it to a test server action so itā€™s easier to test. This is my set up now:

This is what I get when I run this server action:

{
  "inputdata": [
    {
      "id": 13,
      "ac_tags": "tag1, tag2"
    },
    {
      "id": 14,
      "ac_tags": "tag3"
    }
  ],
  "repeat": [
    {
      
    },
    {
      
    }
  ]
}

So first question: what am I doing wrong with your one liner now?

The problem is the repeat itself :slight_smile: You donā€™t need it.
Just use the RunJS action with the one liner, add the input data as tags variable, and Bobā€™s your uncle.

Damn. Yes this is what I need, and no the global variable is redundant now. I was trying to get that to work because of the scope of the repeat.

Thanks, this is really nice.

I love these kind of one liners and want to learn more. Iā€™ve been looking at the map function the last few weeks but still donā€™t quite grasp it.

Can you explain your code a little bit?
This is my take so far:

For easy reading Iā€™m putting the object into a javascript variable

let tags = [
    {
      "id": 13,
      "ac_tags": "tag1, tag2"
    },
    {
      "id": 14,
      "ac_tags": "tag3"
    }
  ];

return [tags.map((e) => e.ac_tags.replace(/\s/g, '')).join(',')]` //result: "processInputData": ["tag1,tag2,tag3" ]

Taken apart:
Map is creating a new array, populated with the results of the function we call on every element in the array. So that function is:

(e) => e.ac_tags.replace(/\s/g, '')
  1. (e) = current processed element, so in the first run that would be:
{
      "id": 13,
      "ac_tags": "tag1, tag2"
    }
  1. The arrow notation is short for:
function(e) {
  return e.ac_tags.replace(/\s/g, ''); //result: "processInputData": ["tag1, tag2"]
}

Where /\s/g is RegEx matching white space (\s) and stop after the first match (/g)
So now the beginning white space is replaced with '' (nothing) if itā€™s found.

  1. Weā€™re still in the map, so itā€™s doing this for each object in the array. So now we have:
let tags = [
    {
      "id": 13,
      "ac_tags": "tag1,tag2"
    },
    {
      "id": 14,
      "ac_tags": "tag3"
    }
  ];
  1. The join(',') is joining the elements with a comma, but now Iā€™d expect this:
["tag1, tag2", "tag3"];

So if Iā€™m correct so far, where am I missing the split of ā€˜tag1ā€™ and ā€˜tag2ā€™?

EDIT:
One more thing, the regex is removing all white spaces - these ones should remain intact:


(I guess I can tweak the RegEx but that is a confusing subject that I really need to invest more time in at some point in my life)

, :slight_smile:

1 Like

The join is done on the map result. Itā€™s not part of the map function.

["tag1,tag2","tag3"].join(',')

returns

"tag1,tag2,tag3"

1 Like