问题描述
我有 neo4j 响应对象(我给你提供了整个响应,所以我会做对):
result = {
"records": [
{
"keys": [
"criteria.name"
],"length": 1,"_fields": [
"Perspective"
],"_fieldLookup": {
"criteria.name": 0
}
},{
"keys": [
"criteria.name"
],"_fields": [
"3D"
],"_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,"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,"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"
],"_fields": [
"3D"
],"_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,"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,"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);