I'm trying to only return array values that contain a specific phrase in the string of 'Corp'
This returns all the values, but I only need the ones that contain "Corp"
var url = "/iaas/api/image-profiles";
System.debug("getImageProfiles url: " url);
var response=System.getModule("pso.vra.util.rest").genericRestAPI(url,null,null);
var responseJSON=JSON.parse(response.contentAsString);
System.log("Response : " JSON.stringify(responseJSON));
var imageProfilesContent=responseJSON.content;
var imageProfiles = [];
System.log("Checking : " Object.keys(responseJSON.content[0].imageMappings.mapping));
var imageProfiles = JSON.stringify(Object.keys(responseJSON.content[0].imageMappings.mapping));
System.log(imageProfiles);
return JSON.parse(imageProfiles);
0 Windows Server 2022
1 Windows Server 2019
2 Windows Server 2022 - Corp
3 Windows Server 2019 - Corp
4 Rhel8 - Corp
5 Rhel8
...
I tried using filter() but could not figure out how to use it.
CodePudding user response:
To use filter I believe you'll want something like this:
filteredImageProfiles = imageProfiles.filter(item => item.includes('Corp'));
I'm not exactly sure what the structure of your object is but that is an example of a filter ^^^. Just make sure you have whatever item
is is a string to test against Corp
Let me know if that isn't helpful though I can come back, but without know the structure of the object this is as close as I can get you.
CodePudding user response:
I'm sure @Rhett's answer will work for someone who know javascript better than I do but I got it to work with a for loop:
function checkCorp() {
for ( i = profiles.length - 1; i >= 0; i--) {
var element = profiles[i];
if (element.toLowerCase().indexOf("Corp".toLowerCase()) == -1) {
profiles.splice(i, 1);
}
}
System.log(profiles);
}