我有 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”。
最佳答案
您可以使用 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);
关于javascript - 解析 neo4j JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67359519/