Simplify the use of multiple Places all

Asked

Viewed 68 times

-1

I got 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(Sponse, null, 4); before applying the various Places 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 after the catataus of Places returns

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

So the question is: How to avoid this heap of Replaces, and assign the result to the variable res

  • 1

    It’s a json, maybe it’s simpler to parse and iterate the elements and play in a string.

  • yes, but how do you do it?

2 answers

3

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

In the case below, I used the 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 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 the Object.values(), it is still experimental and not supported by most browsers.

  • sorry work, I could not play the result inside the variable see

  • @Leocaracciolo edited the answer.

  • but var json would be my first var res? res = JSON.stringify(Response, null, 4);

  • with the idea of replace, regex, Function in the head, I let out the simplest solution that now occurred to me, within the context of the edited question the solution is:: res = (Response.ip + "," + Response.hostname+ "," + Response.city+ "," + Response.Region+ "," + Response.country+ "," + Response.Loc+ "," + Response.org);

  • @Tobiasmesquita well observed when to Object.values. As for its solution, it would possibly be more idiomatic using the reduce, in place of map, don’t you think? Something thus.

  • @Leocaracciolo this is not a good solution when dealing with the return of another application, because at any time the names may be changed and your code will give error.

  • @Andersoncarloswoss I particularly do not like to abuse reduce, for those who do not know it, he is a bit confused, updated the example to use it, see how not cool.

  • like the tip " at any time the names can be changed...."

Show 3 more comments

3


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.

Functioning

Basically we do the analysis of JSON through JSON.parse, creating a Javascript object. Com Object.values We get the list of object attribute values and finally with join unite all values in one 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 code snippet that may be closer to your application. In this case, the value of response has already been analyzed as JSON and therefore does not need to use JSON.parse, the same 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>
<div id="dados"></div>

  • sorry for the job, I couldn’t apply, I re-asked the question.

  • @Leocaracciolo what was the problem?

  • I could not play the result inside the variable see

  • How not? The variable result of the example already returns this value.

  • @Leocaracciolo I added at the end of the reply a example, see if it’s not something you want.

  • What a wonder, simplified more than I asked, congratulations, saved several lines, the code was summarized to 6 lines. Only one more question - it is necessary to declare the variable res?

  • @Leocaracciolo no, could be used directly: $("#dados").html("Dados: " + Object.values(response).join());

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.