1

I'm struggling to find a way to cut down this JSON array so I can use the values remaining. Below is what's returned to me from the API I'm fetching and I'm looking to remove the excess so I just have an array with the LGA results. Really unsure where to even start.

{
  "query": {
    "offence": "Dangerous Operation of a Vehicle",
    "area": "",
    "age": "",
    "year": "",
    "gender": ""
  },
  "result": [
    {
      "LGA": "Aurukun Shire Council",
      "total": 156,
      "lat": -13.354875,
      "lng": 141.729058
    },
    {
      "LGA": "Balonne Shire Council",
      "total": 99,
      "lat": -28.464607,
      "lng": 148.189292
    },
    {
      "LGA": "Yarrabah Shire Council",
      "total": 28,
      "lat": -16.910135,
      "lng": 145.868659
    }
  ]
}
3
  • Question is not clear. What is the expected output? Commented Jun 1, 2019 at 15:28
  • Please provive a really question => stackoverflow.com/help/how-to-ask Commented Jun 1, 2019 at 15:41
  • Please remove the extra data and only keep the data required to clarify your question. Commented Jun 1, 2019 at 19:24

5 Answers 5

1

You can try:

response.data.result = response.data.result.slice(1)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array​.prototype​.map function to transform your result:

const response = getYourJsonFromAPI()
response.result.map(result => result.LGA)

which returns:

["Aurukun Shire Council", "Balonne Shire Council", "Yarrabah Shire Council"]

Comments

0

I'm not sure how you get the response from API, but you can try:

console.log(response.data.result);

Comments

0

for example, if you want only LGA values, you could use something like that:

let parsedJson = JSON.parse(json);                //parse json to object
let onlyLGAValues = {};                           //create a new dictionary

parsedJson.result.forEach(function(key, index){   //for each result
    onlyLGAValues[index] = key.LGA;               //push the LGA value in the new dictionary
}); 

console.log(onlyLGAValues);

expected output:

{0: "Aurukun Shire Council", 1: "Balonne Shire Council", ... 77: "Yarrabah Shire Council"}

Comments

0

If I am reading your question correctly, you want to get a string array containing the LGA properties:

const data = {
  query: {
    offence: "Dangerous Operation of a Vehicle",
    area: "",
    age: "",
    year: "",
    gender: ""
  },
  result: [{}, {}] // your current large array
};

const lgaArray = data.result.map(({ LGA }) => ({ LGA }));

console.table(lgaArray);

A screenshot to give a visual aid:

lga map array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.