-4

The following API returns some JSON data:

https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=bananas

I am trying to push the properties (also objects) of the Pages object into a single array. Each object should be its own element in the array.

For example query.pages should be pushed into an array like [38940{}, 284306{}, ...]

Im doing this so that I can run through the array using ForEach and populate the DOM with the required data.

Thanks in advance!

Nims
  • 195
  • 2
  • 2
  • 11
  • 4
    Please show what you have tried. Stackoverflow is a not a free code writing service. The objective here is to help you fix ***your code*** when it doesn't work as expected – charlietfl Apr 01 '17 at 23:09

2 Answers2

0

Seems that it is all you need if you have ES6:

var pages = Object.values(response.query.pages);

Or you can use this variant to get it work in older browsers:

var pages = Object.keys(response.query.pages).map(function (key) {
    return response.query.pages[key];
});

More detailed answer can be found here.

Community
  • 1
  • 1
Aquajet
  • 744
  • 8
  • 13
-3
// result is the json from https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=bananas

const singleArray = Object.keys(result.query.pages).reduce((acc, id) => {
  acc.push(result.query.pages[id]);
  return acc;
}, []);
topheman
  • 7,422
  • 4
  • 24
  • 33