🚀 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