1

I have a webpage that takes a JSON via var url = myReport.json and displays information. The information it displays are server information that I am monitoring.

The server information monitors all applications running on the server, but it filters back ones that are named company_name_ and looks like this:

company_name_app1
company_name_app2
company_name_app3
company_name_app4
company_name_app5

On the html page, I want to remove the strings company_name_ so that the webpage will just display the following:

app1
app2
app3
app4
app5

Is there a way to remove a specified string match from the JSON entries so I can display it as wanted above?

EDIT:

<script>
    //define a function that will fetch status and set icon URL
    function setServerProcessesServer1761() {
        var url = "Server1761.json";
        var $svc = $("#Server1761-processes"); //get a reference to your <div>

        $.getJSON(url, function(data) {
            document.getElementById("Server1761-processes").innerHTML = data.processes.filter(s => s.Name.value.includes("company_name")).map(s => `<tr><td>${s.Name.value}</td> <td>${s.Status}</td></tr>`).join('\n');
            $('#Server1761-processes').html(function (_, html) { return html.replace(/runn1ng/g,"<img src='smallgreen.png' />")});
            $('#Server1761-processes').html(function (_, html) { return html.replace(/fai1ed/g,"<img src='smallred.png' />")});
        }); // you will have to concatenate the entire services request line on line 130
    }

    //special jQuery syntax to run when document is ready (like onload but better)
    $(function() {
        setServerProcessesServer1761();
    });

</script>
Lasagna Cat
  • 297
  • 3
  • 24
  • Possible duplicate of [Javascript how to remove text from a string](https://stackoverflow.com/questions/10398931/javascript-how-to-remove-text-from-a-string) – A l w a y s S u n n y Aug 09 '17 at 16:23

1 Answers1

2
s.Name.value.replace("company_name_", "");

The above will replace company_name_part in the string with nothing. It uses the replace method from the String object.

String.replace(input (String, Regex), replace (String, Function))

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • Hi Mouser, thank you for the feedback. Where would I insert this? Do I need to wrap it in a function? Or declare a variable? I updated my post with my ` – Lasagna Cat Aug 09 '17 at 16:32
  • Hi Mouser, sorry I am having difficulty understanding this solution. I notice you edited your post, but where exactly does it need to be inserted in order to work? Again I am new to JavaScript I am sorry I am having difficulty understanding your solution. Thank you again! – Lasagna Cat Aug 09 '17 at 16:41
  • In the first line after `$_getJSON` there is a `s.Name.value` just after the `=>` – Mouser Aug 09 '17 at 16:48
  • Thank you again Mouser. So I got it to work and inserted it in the `s.Name.value` of line like you recommended, but I noticed it only applies to the first entry. If I want it to apply to the rest of my entries, I have to continue to write `.replace("company_name_", "")` for all entries. Since I have 5, I ended up writing `.replace("company_name_", "").replace("company_name_", "").replace("company_name_", "").replace("company_name_", "").replace("company_name_", "");`. Is there a way to add a loop into this without rewriting? – Lasagna Cat Aug 09 '17 at 17:10
  • I found the solution: `.replace(/company_name_/g, "");` – Lasagna Cat Aug 09 '17 at 17:42