我正在使用地图(几个地图在嵌套json中获取必要的元素)功能。我正在尝试根据所需模板从Neo4j数据库获取输出。在最后一张地图中,我正在构建所需输出的一部分,并将其存储在内部变量中:
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 }
如您所见,其中一个块的元素段为空,即第二个路径块。我需要的是能够用类型为(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] } } ]
答案 0 :(得分:1)
将此代码放在您正在value
的位置。
const str = `<workzag-jobs>
<position>
<id>116303</id>
<recruitingCategory>Festangestellte</recruitingCategory>
<name>Initiativbewerbung (Festanstellung)</name>
<jobDescriptions>
<jobDescription>
<name>WAS DU MACHEN WIRST</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
<jobDescription>
<name>WAS DU MITBRINGST</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
<jobDescription>
<name>WAS WIR DIR BIETEN</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
</jobDescriptions>
<createdAt>2018-07-31T09:25:04+02:00</createdAt>
</position>
<position>
<id>126073</id>
<recruitingCategory>Festangestellte</recruitingCategory>
<name>Initiativbewerbung (Festanstellung)</name>
<jobDescriptions>
<jobDescription>
<name>Deine Aufgaben</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
<jobDescription>
<name>Dein Profil</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
<jobDescription>
<name>Warum wir?</name>
<value>
<![CDATA[
<p style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p><ul style="color:rgb(0,0,0);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:normal;background-color:rgb(255,255,255);"><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li><li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</li><li>At vero eos et accusam et justo duo dolores et ea rebum.</li><li>Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</li></ul>
]]>
</value>
</jobDescription>
</jobDescriptions>
<createdAt>2018-07-31T09:25:04+02:00</createdAt>
</position>
`;
const doc = new DOMParser().parseFromString(str, 'text/html');
const searchId = "116303";
const filteredIdEle = Array.prototype.find.call(doc.body.querySelectorAll('*'), ((ele) => {
return ele.textContent == searchId;
}));
if(filteredIdEle) {
filteredIdEle.parentElement.querySelectorAll('jobDescription').forEach((ele) => {
const name = ele.querySelector('name').textContent;
const val = ele.querySelector('value').textContent;
console.log("Name :", name);
console.log("Value :", val);
});
}
如果通过执行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
});
});
答案 1 :(得分:-1)
首先,我建议利用已经构建的新数组map()
:
var individual_path = px.segments.map(function(pathSegment){
return {
"start": pathSegment.start.properties.name,
"weight": pathSegment.relationship.properties.Weight.low,
"end": pathSegment.end.properties.name};
};
如果现在为空,只需将其替换为所需的数组即可:
if (individual_path.length === 0) {
individual_path = [ { start: 'A', weight: 0.0, end: 'A' } ];
}
答案 2 :(得分:-1)
# which allows us coding conveniently with this api:
import asyncio
async def test():
print("hello world !")
async def main():
loop = asyncio.get_running_loop()
loop.create_task(test())
asyncio.run(main())