Retrieve the specific array element from parsing JSON that includes a particular phrase

I need help filtering array values that contain the phrase 'Corp'

Currently, all values are being returned, but I only want the ones with "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 attempted to use filter() without success.

Answer №1

If you're looking to implement a filter, you may want to consider the following approach:

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

It's important to ensure that the item being tested against 'Corp' is a string in your object structure.

If you need further assistance or clarification on this topic, feel free to reach out. Without insight into your specific object structure, this example should get you started on the right path.

Answer №2

While @Rhett's solution may be more suitable for those proficient in javascript, I managed to achieve the desired outcome using a simple for loop:

function filterCorpNames() {
  for (j = companyProfiles.length - 1; j >= 0; j--) {
    var company = companyProfiles[j];
    if (company.toLowerCase().indexOf("Corp".toLowerCase()) == -1) {
      companyProfiles.splice(j, 1);
    }
  }
  System.log(companyProfiles);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize Android accelerometer data to bring objects to life with animation

Utilizing an Android app, I am streaming accelerometer data to a Python script on my PC. The data is then saved to a text file. My goal is to utilize Javascript and jQuery to animate a 3D CSS cuboid (representing the device) to replicate the movements capt ...

What is the best way to send a POST request with a JSON body using RestSharp in C#?

When using the post method, I often include parameters in the URL like this: https://localhost:44345/exemple?id=123&value=123&anotherValue=123 However, I would prefer not to display these parameters in the header. Is there a way to send them in t ...

Is there a way to convert a Java object into a JSON format in Java without relying on external libraries or dependencies?

public class Information { private String privilege; private String bar; private Int years; } transformed into {"privilege" : "data", "bar": "data", "years":"data"} devoid of Gson or Jackson. solely using core Java Is there a simpler approac ...

Having trouble displaying the Chrome context menu for a specific Chrome extension

I've read through several posts on this topic, but I'm still struggling to identify the issue with my implementation of the Chrome contextMenu API. I simply copied the code from a tutorial on Chrome APIs, and though there are no errors, the menu ...

The absence of "_ssgManifest.js" and "_buildManifest.js" files in a Next.js application deployed on Google Cloud Platform was discovered

Upon opening the website, there are instances where the console displays the following errors: GET https://example.com/subpath/_next/static/9ufj5kFJf/_buildManifest.js [HTTP/3 404 Not Found 311ms] GET https://example.com/subpath/_next/static/9ufj5kFJf/_ss ...

Steps for automatically toggling a button within a button-group when the page is loaded using jQuery in Bootstrap 3

Here is a button group setup: <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <input type="radio" name="environment" id="staging" value="staging" />Staging </label> <label class= ...

Unlock real-time alerts with the power of JavaScript and PHP!

I am currently working on enhancing my skills in javascript. I have a basic idea of what I want to achieve. My goal is to create an automated javascript function that accesses a php page. This php page will create an array of new notifications for the ja ...

Jade console.log() not functioning properly as anticipated

Is it possible that I can just insert -console.log('hello') into any part of the jade code? It doesn't seem to be working, do you know what could be causing this issue? ...

The mobile web app on iOS is stuck in a never-ending loop of showing the

My mobile app is built using angular.js, Twitter Bootstrap, and grunt with a .NET back end. After logging in, the loading spinner keeps showing up constantly in the top nav next to the time and battery status. We make server calls at login through a factor ...

Elevate to Babel 7: Unable to access the property 'bindings' of null

I recently made the switch to Babel 7 (upgrading from version 6) using the following commands: npm remove babel-cli npm install --save-dev @babel/cli @babel/core @babel/preset-env This is the content of my .babelrc file: { "presets": ["env"] } After th ...

Working with XML files in Node.js

I'm currently running an Arch Linux system with KDE plasma and I have a 50mb XML file that I need to parse. This XML file contains custom tags. Here is an example of the XML: <JMdict> <entry> <ent_seq>1000000</ent_seq&g ...

Idiosyncratic TypeScript behavior: Extending a class with comparable namespace structure

Lately, I've been working on creating a type library for a JavaScript written library. As I was defining all the namespaces, classes, and interfaces, I encountered an error TS2417 with some of the classes. I double-checked for any issues with method o ...

Error message "Unable to access property 'rotation' of an object that does not exist - Three.js"

I'm currently facing an issue where my code is executing as expected, but there are two errors popping up in the console saying "Cannot read property 'rotation' of undefined". It's puzzling because both variables are defined globally. I ...

Issues with HTML5 video playback in Apple machines using the Firefox browser

On a website I'm developing, I've included an HTML5 video that works perfectly except for one specific issue - it won't play on Firefox in Apple devices. Surprisingly, it functions well on all other browsers and even on Firefox in Windows an ...

Uh-oh! An issue arose with a status code of 400 while using Axios in Node.js

I'm having trouble with the API connection using axios Error: Request failed with status code 400 Can anyone offer assistance? This is my services/api.js: const axios = require("axios"); const api = axios.create({ baseURL: "https://url.com" }); ...

What is the typical number of open CLI terminals for a regular workflow?

After experimenting with Gulp, Bower, ExpressJS, and Jade, I have settled on a workflow that I am eager to switch to. The only issue I am facing is the need to have two terminals open simultaneously in order to use this workflow efficiently. One terminal ...

Navigating Angular: Uncover the Secrets of Unwrapping Json Data

I need some help with parsing Json data in Angular TypeScript. The data is structured as follows: https://i.sstatic.net/H7s8q.png When calling a REST API, I want to convert a Java class object into an array of model classes. How can this be achieved? "g ...

Executing an AJAX POST request from one domain (localhost:9393) to another domain (localhost:9696) using Spring Rest

I am encountering an issue with a form on one app running on localhost:9393. The JavaScript function used to post data is: function registerClient() { var postData = $("#client-reg").serializeArray(); var formURL = "http://localhost:9393/mPaws/client/re ...

Cypress and VueJS: How to target elements that are dynamically generated following a specific user interaction

I am currently testing a new feature where a button will only appear for the user to click after they have completed another action. Before proceeding with the action, I am verifying if the button exists: cy.get('span') .contains('Selec ...

"Why is it that the keypress event doesn't function properly when using the on() method

My goal is to capture the enter event for an input field $("input[name='search']").on("keypress", function(e){ if (e.which == '13') { alert('code'); } }); This is the HTML code snippet: <input name="searc ...