简体   繁体   中英

Accessing data within a JSON object using javascript

I have the following JSON object returned and I want to access all the "title" values. I have been able to get a new object of all the "pages," but not the "titles." I have tried everything except the correct thing! The "similar" questions on here are straight forward array access questions. This seems to involve nested arrays. Please help! Thanks.

{
    "batchcomplete": "",
    "continue": {
        "gsroffset": 15,
        "continue": "gsroffset||"
    },
    "query": {
        "pages": {
            "22989": {
                "pageid": 22989,
                "ns": 0,
                "title": "Paris",
                "index": 1
            },
            "59134": {
                "pageid": 59134,
                "ns": 0,
                "title": "Paris Commune",
                "index": 13
            },
            "61371": {
                "pageid": 61371,
                "ns": 0,
                "title": "Paris (disambiguation)",
                "index": 2
            },
            "64129": {
                "pageid": 64129,
                "ns": 0,
                "title": "Catacombs of Paris",
                "index": 11
            },
            "76286": {
                "pageid": 76286,
                "ns": 0,
                "title": "Disneyland Paris",
                "index": 15
            },
            "89106": {
                "pageid": 89106,
                "ns": 0,
                "title": "Paris–Brest–Paris",
                "index": 8
            },
            "357488": {
                "pageid": 357488,
                "ns": 0,
                "title": "Paris Saint-Germain F.C.",
                "index": 10
            },
            "868936": {
                "pageid": 868936,
                "ns": 0,
                "title": "Paris Gun",
                "index": 14
            },
            "2397134": {
                "pageid": 2397134,
                "ns": 0,
                "title": "Paris (Paris Hilton album)",
                "index": 6
            },
            "7618874": {
                "pageid": 7618874,
                "ns": 0,
                "title": "Paris syndrome",
                "index": 9
            },
            "11217925": {
                "pageid": 11217925,
                "ns": 0,
                "title": "Paris Hilton",
                "index": 7
            },
            "23528038": {
                "pageid": 23528038,
                "ns": 0,
                "title": "Paris Jackson (actress)",
                "index": 5
            },
            "30242372": {
                "pageid": 30242372,
                "ns": 0,
                "title": "Paris Agreement",
                "index": 4
            },
            "45259235": {
                "pageid": 45259235,
                "ns": 0,
                "title": "Paris Laxmi",
                "index": 12
            },
            "55340805": {
                "pageid": 55340805,
                "ns": 0,
                "title": "Paris Paris",
                "index": 3
            }
        }
    }
}

It wants me to add more details, but I don't know what else I can add.

Try this:

const data = //your json
const pages = data.query.pages
const titles = Object.keys(pages).map( key => pages[key].title )

Store the values from 'pages' in a variable, then use Object.keys to get as an array all keys within 'pages' and then using map operator you will iterate over those keys and for each one, return the attribute 'title' for this entry.

If you want to use lodash/underscore, something like this would work:

const dataPages = data["query"]["pages"];
let titles = [];
_.each(dataPages, (dataPage) => titles.push(dataPage["title"]))

This should get you a titles array!

This uses Object.keys to enumerate over the pages in the JSON.

"use strict"
const DATA = //your Map
const pages = DATA.query.pages; 
const keys = Object.keys(pages); // returns an array of the ID numbers that are properties in each page
let titles = [keys.length]; // initialize an output array

for(let i=0; i < keys.length; i++){
    titles[i] = pages[keys[i]].title;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM