Simplify the use of multiple replaces all

-1

I have the following

var res;
$(window).load(function(){
  $.get("http://ipinfo.io", function (response) {
     res = JSON.stringify(response, null, 4);
     res = res.replace(/:/g, '');
     res = res.replace(/\"/g, '');
     res = res.replace(/\n/g, '');
     res = res.replace("{", "");  
     res = res.replace("}", ""); 
     res = res.replace("ip", ""); 
     res = res.replace("hostname", "");
     res = res.replace("city", "");
     res = res.replace("region", "");
     res = res.replace("country", "");
     res = res.replace("loc", "");
     res = res.replace("org", "");
     res = res.replace(/ /g, '');

     $("#dados").html("Dados: " + res); 

   }, "jsonp");

});
  

res = JSON.stringify (response, null, 4); before applying the various replaces returns

{
"ip": "187.155.67.279",
"hostname": "b1c33eb3.virtua.com.br",
"city": "Leblon",
"region": "Rio de Janeiro",
"country": "BR",
"loc": "-22.8244,-43.0353",
"org": "AS28573 CLARO S.A."
}
  

And then the replaces catataus returns

187.155.67.279,b1c33eb3.virtua.com.br,Leblon,Rio de Janeiro,BR,-22.8244,-43.0353,AS28573 CLARO S.A.

Then the question is: How to avoid this heap of replaces, and assign the result in the variable res

    
asked by anonymous 19.04.2017 / 19:12

2 answers

4

Considering the text:

const value = '{
  "ip": "187.155.67.279",
  "hostname": "b1c33eb3.virtua.com.br",
  "city": "Leblon",
  "region": "Rio de Janeiro",
  "country": "BR",
  "loc": "-22.8244,-43.0353",
  "org": "AS28573 CLARO S.A."
}';

You can do:

var result = Object.values(JSON.parse(value)).join();

The result of console.log(result) is:

187.155.67.279,b1c33eb3.virtua.com.br,Leblon,Rio de Janeiro,BR,-22.8244,-43.0353,AS28573 CLARO S.A.

Running

We basically do JSON parsing through JSON.parse , creating a JavaScript object. With Object.values we get the list of the values of the attributes of the object and finally, with join we join all the values in a single string .

const value = '{
    "ip": "187.155.67.279",
    "hostname": "b1c33eb3.virtua.com.br",
    "city": "Leblon",
    "region": "Rio de Janeiro",
    "country": "BR",
    "loc": "-22.8244,-43.0353",
    "org": "AS28573 CLARO S.A."
}';

var result = Object.values(JSON.parse(value)).join();

console.log(result);

Example

A snippet of code that may be closer to your application. In this case, the value of response has already been parsed as JSON and thus does not need to use JSON.parse , it is already a JavaScript object.

$(window).load(function(){
  $.get("https://ipinfo.io", function (response) {
  
     let res = Object.values(response).join();
     
     $("#dados").html("Dados: " + res); 
     
  }, "jsonp");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="dados"></div>
    
19.04.2017 / 19:44
4

You can transform the string into an object using JSON.parse , so you can get all the keys of the object using the Object.key() method, finally just scroll through the keys with a forEach .

In the case below, I used map instead of forEach because I wanted to map the values to a new array.

var json = '{ \
  "ip": "187.155.67.279", \
  "hostname": "b1c33eb3.virtua.com.br", \
  "city": "Leblon", \
  "region": "Rio de Janeiro", \
  "country": "BR", \
  "loc": "-22.8244,-43.0353", \
  "org": "AS28573 CLARO S.A." \
}';

var _obj = JSON.parse(json);
var keys = Object.keys(_obj);
var values = keys.map(function (key, indice) {
  console.log(key + ": " + _obj[key]);
  return _obj[key];
});
console.log(values);

EDIT

You already receive an object, so there is no need to parse.

$.get("http://ipinfo.io", function (response) {
    res = Object.keys(response).map(function (key) {
        return response[key];
    }).join();
    $("#dados").html("Dados: " + res);
}, "jsonp");

see it working.:

var json = '{ \
  "ip": "187.155.67.279", \
  "hostname": "b1c33eb3.virtua.com.br", \
  "city": "Leblon", \
  "region": "Rio de Janeiro", \
  "country": "BR", \
  "loc": "-22.8244,-43.0353", \
  "org": "AS28573 CLARO S.A." \
}';

var response = JSON.parse(json);
var res = Object.keys(response).map(function (key, indice) {
  return response[key];
}).join();

console.log(res);

// usando o reduce (elimina a necessidade do join, mas o codigo fica menos legivel).
res = Object.keys(response).reduce(function (keyA, keyB) {
  return (response[keyA] || keyA) + "," + response[keyB];
});

console.log(res);

P.S.: Avoid using Object.values() , it is still experimental and is not supported by most browsers.

    
19.04.2017 / 19:43