0

I need to get the keys for the values of an object that I got from an API. For example, this is the console when I console.log() the object.

{links: {…}, spkId: "3012393", designation: "1979 XB", sentryId: "bJ79X00B", fullname: "(1979 XB)", …}
links: {near_earth_object_parent: "http://www.neowsapp.com/rest/v1/neo/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM", self: "http://www.neowsapp.com/rest/v1/neo/sentry/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM"}
spkId: "3012393"
designation: "1979 XB"
sentryId: "bJ79X00B"
fullname: "(1979 XB)"
year_range_min: "2056"
year_range_max: "2113"
potential_impacts: "2"
impact_probability: "7.36e-07"
v_infinity: "23.9194972826087"
absolute_magnitude: "18.53"
estimated_diameter: "0.662"
palermo_scale_ave: "-2.82"
Palermo_scale_max: "-3.12"
torino_scale: "0"
last_obs: "1979-Dec-15.42951"
last_obs_jd: "2444222.92951"
url_nasa_details: "https://cneos.jpl.nasa.gov/sentry/details.html#?des=1979+XB"
url_orbital_elements: "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3012393;orb=1"
is_active_sentry_object: true
average_lunar_distance: 14.2337856843
__proto__: Object

I need to get the text value of links, spkId, designation, sentryId, etc...

FinnWithIt
  • 65
  • 8
  • use `Object.keys(obj)` – Sunil Lama Mar 08 '20 at 04:17
  • Does this answer your question? [best way to get the key of a key/value javascript object](https://stackoverflow.com/questions/6268679/best-way-to-get-the-key-of-a-key-value-javascript-object) – Agney Mar 08 '20 at 04:44

2 Answers2

0

let items ={
  links: {near_earth_object_parent: "http://www.neowsapp.com/rest/v1/neo/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM", self: "http://www.neowsapp.com/rest/v1/neo/sentry/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM"},
spkId: "3012393",
designation: "1979 XB",
sentryId: "bJ79X00B",
fullname: "(1979 XB)",
year_range_min: "2056",
year_range_max: "2113",
potential_impacts: "2",
impact_probability: "7.36e-07",
v_infinity: "23.9194972826087",
absolute_magnitude: "18.53",
estimated_diameter: "0.662",
palermo_scale_ave: "-2.82",
Palermo_scale_max: "-3.12",
torino_scale: "0",
last_obs: "1979-Dec-15.42951",
last_obs_jd: "2444222.92951",
url_nasa_details: "https://cneos.jpl.nasa.gov/sentry/details.html#?des=1979+XB",
url_orbital_elements: "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3012393;orb=1",
is_active_sentry_object: true,
average_lunar_distance: 14.2337856843
}

let keys = Object.keys(items)
console.log(keys)

use this code you will get the array of key's text of that object and use map to print it on the UI


    let items ={
      links: {near_earth_object_parent: "http://www.neowsapp.com/rest/v1/neo/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM", self: "http://www.neowsapp.com/rest/v1/neo/sentry/3012393?api_key=IrN5Iyve4fiJfxQFchJkCow2zPtW9yGjPzcrgFlM"},
    spkId: "3012393",
    designation: "1979 XB",
    sentryId: "bJ79X00B",
    fullname: "(1979 XB)",
    year_range_min: "2056",
    year_range_max: "2113",
    potential_impacts: "2",
    impact_probability: "7.36e-07",
    v_infinity: "23.9194972826087",
    absolute_magnitude: "18.53",
    estimated_diameter: "0.662",
    palermo_scale_ave: "-2.82",
    Palermo_scale_max: "-3.12",
    torino_scale: "0",
    last_obs: "1979-Dec-15.42951",
    last_obs_jd: "2444222.92951",
    url_nasa_details: "https://cneos.jpl.nasa.gov/sentry/details.html#?des=1979+XB",
    url_orbital_elements: "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3012393;orb=1",
    is_active_sentry_object: true,
    average_lunar_distance: 14.2337856843
    }

    let keys = Object.keys(items)
    console.log(keys)

Hashim aslam
  • 243
  • 1
  • 2
  • 7
0

To get the keys in an array format use Object.keys() method.

let allKeys = Object.keys(items);
console.log(allKeys)

// Output :-
// ["links", "spkId", "designation", "sentryId", "fullname", "year_range_min", "year_range_max", "potential_impacts", "impact_probability", "v_infinity", "absolute_magnitude", "estimated_diameter", "palermo_scale_ave", "Palermo_scale_max", "torino_scale", "last_obs", "last_obs_jd", "url_nasa_details", "url_orbital_elements", "is_active_sentry_object", "average_lunar_distance"]

And if you want to iterate over the keys and get the values also.

for(key in items)
{
    // This will give you key(name).
    console.log(key);
    // This will output the value of that particular key.
    console.log(items[key]);
}

// Output :-
// spkId
// 3012393
// designation
// 1979 XB
// sentryId
// bJ79X00B
Anurodh Singh
  • 814
  • 5
  • 9