解析 neo4j 的 JSON 响应

3
我有neo4j响应对象(我把整个响应提供给你,以便我弄对):
result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

我需要从中仅获取各个 "_fields",如下所示:
{"Perspective", "3D", "2D"}

我该怎么做?

使用以下代码,可以成功获取任何记录的值:

a = Object.values(result.records)
b = Object.values(a[0]._fields)
console.log(b);

但我不明白如何遍历数组中的每个"a"元素并提取"_fields"。

1个回答

2
你可以使用 for ... of 循环遍历 records 对象中的所有内容!

const result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

let option1 = [];
let option2 = [];

// For each object in result.records,
for (let val of result.records) {
  // Put everything in val._fields into our result array.
  // ... spreads the array, so all elements are inserted
  // individually in case, in the future,
  // there are multiple items in _fields.
  option1.push(...val._fields);
  
  // For the object you provided, you could just do
  // val._fields[0]. However, option1 is more generalizable
  // in case there's ever more than one thing in _fields.
  option2.push(val._fields[0]);
}

console.log(option1);
console.log(option2);


2
谢谢!我知道这很简单,但作为新手,我非常感激你的帮助。我错误地使用了“for ... in”而不是“for ... of”。 - Roman Korobko
哦,我已经做了那么多次了,即使现在... :) 祝你好运! - Nisala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接