gpt4 book ai didi

javascript - 替换数组数组中的空数组

转载 作者:行者123 更新时间:2023-11-30 06:50:12 25 4
gpt4 key购买 nike

我正在使用 map (几个 map 在嵌套的 json 中获取必要的元素)函数。我正在尝试根据所需模板从 Neo4j 数据库获取输出。在上一张 map 中,我正在构建所需输出的一部分,并将其存储在变量中:

 px.segments.map(function(pathSegment){                                                                                          
individual_path.push({
"start": pathSegment.start.properties.name,
"weight": pathSegment.relationship.properties.Weight.low,
"end": pathSegment.end.properties.name});
})

然后我试试:

 console.log(individual_path);

我收到以下响应(因为其中一条记录在数据库中具有空值):

[ { start: 'A', weight: 0.6180339887498948, end: 'P2' } ]
[]
[ { start: 'P1', weight: 0.6180339887498948, end: 'A' },
{ start: 'A', weight: 0.6180339887498948, end: 'P2' } ]
[ { start: 'P1', weight: 0.6180339887498948, end: 'A' } ]

我的问题是,如何用非空替换空数组(同时遍历记录集),如下所示:

[ { start: 'A', weight: 0.0, end: 'A' } ]

最后有这样的东西:

[ { start: 'A', weight: 0.6180339887498948, end: 'P2' } ]
*[ { start: 'A', weight: 0.0, end: 'A' } ]*
[ { start: 'P1', weight: 0.6180339887498948, end: 'A' },
{ start: 'A', weight: 0.6180339887498948, end: 'P2' } ]
[ { start: 'P1', weight: 0.6180339887498948, end: 'A' } ]

我不是很清楚,我将添加 console.log(px);输出:

Path {
start:
Node {
identity: Integer { low: 1, high: 0 },
labels: [ 'concept' ],
properties: { name: 'A', type: 'string' } },
end:
Node {
identity: Integer { low: 2, high: 0 },
labels: [ 'concept' ],
properties: { name: 'P2', type: 'string' } },
segments:
[ PathSegment { start: [Object], relationship: [Object], end: [Object] } ],
length: 1 }
Path {
start:
Node {
identity: Integer { low: 1, high: 0 },
labels: [ 'concept' ],
properties: { name: 'A', type: 'string' } },
end:
Node {
identity: Integer { low: 1, high: 0 },
labels: [ 'concept' ],
properties: { name: 'A', type: 'string' } },
segments: [],
length: 0 }
Path {
start:
Node {
identity: Integer { low: 0, high: 0 },
labels: [ 'concept' ],
properties: { name: 'P1', type: 'string' } },
end:
Node {
identity: Integer { low: 2, high: 0 },
labels: [ 'concept' ],
properties: { name: 'P2', type: 'string' } },
segments:
[ PathSegment { start: [Object], relationship: [Object], end: [Object] },
PathSegment { start: [Object], relationship: [Object], end: [Object] } ],
length: 2 }
Path {
start:
Node {
identity: Integer { low: 0, high: 0 },
labels: [ 'concept' ],
properties: { name: 'P1', type: 'string' } },
end:
Node {
identity: Integer { low: 1, high: 0 },
labels: [ 'concept' ],
properties: { name: 'A', type: 'string' } },
segments:
[ PathSegment { start: [Object], relationship: [Object], end: [Object] } ],
length: 1 }

如您所见,其中一个 block 有一个空元素段,即第二个路径 block 。我需要的是能够用类型为 (start: '', weight: , end:'') 的对象替换空元素

这是 console.log(px.segments);:

[ PathSegment {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
relationship:
Relationship {
identity: [Object],
start: [Object],
end: [Object],
type: 'link',
properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] } } ]
[]
[ PathSegment {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
relationship:
Relationship {
identity: [Object],
start: [Object],
end: [Object],
type: 'link',
properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] } },
PathSegment {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
relationship:
Relationship {
identity: [Object],
start: [Object],
end: [Object],
type: 'link',
properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] } } ]
[ PathSegment {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
relationship:
Relationship {
identity: [Object],
start: [Object],
end: [Object],
type: 'link',
properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] } } ]

最佳答案

将这段代码放在你正在做的地方 console.log()

if (!individual_path.length){ 
individual_path.push({ start: 'A', weight: 0.0, end: 'A' });
}

如果通过执行 console.log() 给出一个空数组 [] 那么它是 length == 0 我们知道 0 == false 所有这些都是真的。

此外,在返回数组时使用 .map。目前,forEach 似乎可以完成这项工作。

px.segments.forEach((pathSegment) => {                                                                                          
individual_path.push({
"start": pathSegment.start.properties.name,
"weight": pathSegment.relationship.properties.Weight.low,
"end": pathSegment.end.properties.name
});
});

我的意思的活生生的例子

let individual_path = [];
if (!individual_path.length) {
individual_path.push({ start: 'A', weight: 0.0, end: 'A' });
}
console.log(individual_path) // should give that object

所以你的代码变成了

if (!individual_path.length){ 
individual_path.push({ start: 'A', weight: 0.0, end: 'A' });
}
console.log(individual_path);

关于javascript - 替换数组数组中的空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com