从JSON解析返回包含某个短语的数组值



我试图只返回包含'Corp'字符串中特定短语的数组值

返回所有值,但我只需要包含"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
...

我试着使用filter(),但是不知道如何使用。

要使用过滤器,我相信你会想要这样做:

filteredImageProfiles = imageProfiles.filter(item => item.includes('Corp'));

我不确定你的对象的结构是什么,但这是一个过滤器^^^的例子。只要确保item是一个字符串就可以对Corp进行测试

如果没有帮助,请告诉我,虽然我可以回来,但如果不知道对象的结构,这是我能给你的最接近的。

我相信@Rhett的答案会对比我更了解javascript的人有效,但我让它与for循环一起工作:

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);
}

最新更新