Remove part of private data

Post Reply
Arsen
Newbie
Posts: 5
Joined: Fri Apr 10, 2020 11:08 pm

Remove part of private data

Post by Arsen »

Hi.

I have a private data looks like abc: 1; def: 2; ghi: ; jkl: 4; xyz: ; and trying to delete everything without digit value using script expression:

const allCities:string = await job.getPrivateData("allCities");
const allCitiesArray:string [] = allCities.split(";");
const pattern = /\b\d+\b/;
const filteredArray = allCitiesArray.filter((item) => pattern.test(item)).toString();
await job.log(LogLevel.Debug, filteredArray);
return filteredArray;

As a result, I would like to get - abc: 1; def: 2; jkl: 4;


What's wrong in my code or do we have other way to do this?
tdeschampsBluewest
Member
Posts: 37
Joined: Tue Jun 01, 2021 11:57 am

Re: Remove part of private data

Post by tdeschampsBluewest »

Hi Arsen,
Hope you are well ?
Your code work great, but it will return an array with the ".toString()" which is not the same format as the one prompted in the first place.
Since i think you want a concatenate string as the one in the private data, you need to use "join(" ;") instead of ".toString()"

Code: Select all

const allCities:string = "abc: 1; def: 2; ghi: ; jkl: 4; xyz: ;"// await job.getPrivateData("allCities");
const allCitiesArray:string [] = allCities.split(";");
const pattern = /\b\d+\b/; ///\w\:\s\d+\b/;
const filteredArray = allCitiesArray.filter((item) => pattern.test(item)).join("; ");
await job.log(LogLevel.Debug, filteredArray);
return filteredArray;
 
TypeScript lang example
Arsen
Newbie
Posts: 5
Joined: Fri Apr 10, 2020 11:08 pm

Re: Remove part of private data

Post by Arsen »

Merci beaucoup, Thomas.

I'm learning to use the scripts in the Switch a little at a time.
Now everything works as I wanted!
Post Reply