我需要在javascript中的嵌套对象中找到包含值“原因”的键,该对象的格式如下:
["del517",["dependency",[["posTag","di__gender=masc|number=sing|prontype=ind"],["edge","det"]]],["dependents",null],["parent","genere259"],["form",[["string","del517","del"]]],["synCat",[["lexClass","preposition"]]],["lexId","di"]]
["genere259",["footprints",["synXGiveRiseToYCxn"]],["parent","comportare259"],["dependents",["del517"]],["form",[["string","genere259","genere"]]],["dependency",[["posTag","s__gender=masc|number=sing"],["edge","nsubj"]]],["referent","?cause3"]]我设法使用了这个函数:
function filterIt(arr, searchKey) {
return arr.filter(function(obj) {
return Object.keys(obj).some(function(key) {
return obj[key].includes(searchKey);
})
});
}我称其为:
console.log("find 'cause'", filterIt(string_transient,"?cause3"));这行得通,我得到了正确的答案:
"find 'cause'", [["genere269", ["footprints", ["synXGiveRiseToYCxn"]], ["parent", "comportare269"], ["dependents", ["del537"]], ["form", [["string", "genere269", "genere"]]], ["dependency", [["posTag", "s__gender=masc|number=sing"], ["edge", "nsubj"]]], ["referent", "?cause3"]]]问题是,我想要找到所有的“原因”值,而不仅仅是像本例中那样后跟int3的值。
如果我通过以下代码调用该函数:
console.log("find 'cause'", filterIt(string_transient,"?cause"));或通过以下方式:
console.log("find 'cause'", filterIt(string_transient,"cause"));它不再起作用了。你知道解决这个问题的方法吗?
您可以在此处找到正确的原始输入:
[
[
"root",
[
"boundaries",
[
[
"si1",
0,
1
],
[
"nota1",
1,
2
],
[
"immediatamente1",
2,
3
],
[
"come1",
3,
4
],
[
"un1",
4,
5
],
[
"meccanismo1",
5,
6
],
[
"del1",
6,
7
],
[
"genere1",
7,
8
],
[
"possa1",
8,
9
],
[
"comportare1",
9,
10
],
[
"automaticamente1",
10,
11
],
[
"una1",
11,
12
],
[
"divergenza1",
12,
13
],
[
"radicale1",
13,
14
],
[
"nella1",
14,
15
],
[
"distribuzione1",
15,
16
],
[
"del2",
16,
17
],
[
"capitale1",
17,
18
],
[
".1",
18,
19
]
]
],
[
"form",
[
[
"sequence",
"si1",
"nota1",
"immediatamente1",
"come1",
"un1",
"meccanismo1",
"del1",
"genere1",
"possa1",
"comportare1",
"automaticamente1",
"una1",
"divergenza1",
"radicale1",
"nella1",
"distribuzione1",
"del2",
"capitale1",
".1"
],
[
"string",
".1",
"."
],
[
"string",
"capitale1",
"capitale"
],
[
"string",
"distribuzione1",
"distribuzione"
],
[
"string",
"radicale1",
"radicale"
],
[
"string",
"divergenza1",
"divergenza"
],
[
"string",
"automaticamente1",
"automaticamente"
],
[
"string",
"genere1",
"genere"
],
[
"string",
"meccanismo1",
"meccanismo"
],
[
"string",
"immediatamente1",
"immediatamente"
],
[
"string",
"nota1",
"nota"
],
[
"string",
"si1",
"si"
],
[
"meets",
"capitale1",
".1",
"?unit921"
],
[
"meets",
"del2",
"capitale1",
"?unit920"
],
[
"meets",
"distribuzione1",
"del2",
"?unit919"
],
[
"meets",
"nella1",
"distribuzione1",
"?unit918"
],
[
"meets",
"radicale1",
"nella1",
"?unit917"
],
[
"meets",
"divergenza1",
"radicale1",
"?unit916"
],
[
"meets",
"una1",
"divergenza1",
"?unit915"
],
[
"meets",
"automaticamente1",
"una1",
"?unit914"
],
[
"meets",
"comportare1",
"automaticamente1",
"?unit913"
],
[
"meets",
"possa1",
"comportare1",
"?unit912"
],
[
"meets",
"genere1",
"possa1",
"?unit911"
],
[
"meets",
"del1",
"genere1",
"?unit910"
],
[
"meets",
"meccanismo1",
"del1",
"?unit909"
],
[
"meets",
"un1",
"meccanismo1",
"?unit908"
],
[
"meets",
"come1",
"un1",
"?unit907"
],
[
"meets",
"immediatamente1",
"come1",
"?unit906"
],
[
"meets",
"nota1",
"immediatamente1",
"?unit905"
],
[
"meets",
"si1",
"nota1",
"?unit904"
]
]
],
[
"meaning",
null
],
[
"semCat",
null
],
[
"synCat",
null
]
],
[
"si1",
[
"parent",
"nota1"
],
[
"dependents",
null
],
[
"form",
[
[
"string",
"si1",
"si"
]
]
],
[
"dependency",
[
[
"posTag",
"pc__clitic=yes|person=3|prontype=prs"
],
[
"edge",
"expl"
]
]
]
],
[
"nota1",
[
"parent",
null
],
[
"dependents",
[
"si1",
"immediatamente1",
"meccanismo1",
"comportare1",
".1"
]
],
[
"form",
[
[
"string",
"nota1",
"nota"
]
]
],
[
"dependency",
[
[
"posTag",
"v__mood=ind|number=sing|person=3|tense=pres|verbform=fin"
],
[
"edge",
"root"
]
]
]
],
[
"immediatamente1",
[
"parent",
"nota1"
],
[
"dependents",
null
],
[
"form",
[
[
"string",
"immediatamente1",
"immediatamente"
]
]
],
[
"dependency",
[
[
"posTag",
"b___"
],
[
"edge",
"advmod"
]
]
]
],
[
"come1",
[
"dependency",
[
[
"posTag",
"e___"
],
[
"edge",
"case"
]
]
],
[
"dependents",
null
],
[
"parent",
"meccanismo1"
],
[
"form",
[
[
"string",
"come1",
"come"
]
]
],
[
"synCat",
[
[
"lexClass",
"conjunction"
]
]
],
[
"lexId",
"come"
]
],
[
"un1",
[
"dependency",
[
[
"posTag",
"ri__definite=ind|gender=masc|number=sing|prontype=art"
],
[
"edge",
"det"
]
]
],
[
"dependents",
null
],
[
"parent",
"meccanismo1"
],
[
"form",
[
[
"string",
"un1",
"un"
]
]
],
[
"synCat",
[
[
"lexClass",
"undefArticle"
]
]
],
[
"lexId",
"un"
]
],
[
"meccanismo1",
[
"parent",
"nota1"
],
[
"dependents",
[
"come1",
"un1"
]
],
[
"form",
[
[
"string",
"meccanismo1",
"meccanismo"
]
]
],
[
"dependency",
[
[
"posTag",
"s__gender=masc|number=sing"
],
[
"edge",
"obl"
]
]
]
],
[
"del1",
[
"dependency",
[
[
"posTag",
"di__gender=masc|number=sing|prontype=ind"
],
[
"edge",
"det"
]
]
],
[
"dependents",
null
],
[
"parent",
"genere1"
],
[
"form",
[
[
"string",
"del1",
"del"
]
]
],
[
"synCat",
[
[
"lexClass",
"preposition"
]
]
],
[
"lexId",
"di"
]
],
[
"genere1",
[
"footprints",
[
"synXGiveRiseToYCxn"
]
],
[
"parent",
"comportare1"
],
[
"dependents",
[
"del1"
]
],
[
"form",
[
[
"string",
"genere1",
"genere"
]
]
],
[
"dependency",
[
[
"posTag",
"s__gender=masc|number=sing"
],
[
"edge",
"nsubj"
]
]
],
[
"referent",
"?cause3"
]
],
[
"possa1",
[
"dependency",
[
[
"posTag",
"vm__mood=sub|number=sing|person=3|tense=pres|verbform=fin"
],
[
"edge",
"aux"
]
]
],
[
"dependents",
null
],
[
"parent",
"comportare1"
],
[
"form",
[
[
"string",
"possa1",
"possa"
]
]
],
[
"synCat",
[
[
"lexClass",
"verb"
],
[
"modal",
""
],
[
"condizionale",
""
]
]
],
[
"lexId",
"potere"
]
],
[
"comportare1",
[
"synCat",
[
[
"condizionale",
""
],
[
"lexClass",
"verb"
]
]
],
[
"referent",
"?frame3"
],
[
"semValence",
[
[
"actor",
"?cause3"
],
[
"theme",
"?effect3"
]
]
],
[
"meaning",
[
[
"frame",
"causation",
"comportare",
"?frame3"
],
[
"slot",
"cause",
"?frame3",
"?cause3"
],
[
"slot",
"effect",
"?frame3",
"?effect3"
]
]
],
[
"semCat",
[
[
"frame",
"causation"
]
]
],
[
"lexId",
"comportare"
],
[
"form",
[
[
"string",
"comportare1",
"comportare"
]
]
],
[
"parent",
"nota1"
],
[
"dependents",
[
"genere1",
"possa1",
"automaticamente1",
"divergenza1",
"distribuzione1"
]
],
[
"dependency",
[
[
"posTag",
"v__verbform=inf"
],
[
"edge",
"conj"
]
]
],
[
"footprints",
[
"synGiveRiseToLexCxn",
"synXGiveRiseToYCxn"
]
]
],
[
"automaticamente1",
[
"parent",
"comportare1"
],
[
"dependents",
null
],
[
"form",
[
[
"string",
"automaticamente1",
"automaticamente"
]
]
],
[
"dependency",
[
[
"posTag",
"b___"
],
[
"edge",
"advmod"
]
]
]
],
[
"una1",
[
"dependency",
[
[
"posTag",
"ri__definite=ind|gender=fem|number=sing|prontype=art"
],
[
"edge",
"det"
]
]
],
[
"dependents",
null
],
[
"parent",
"divergenza1"
],
[
"form",
[
[
"string",
"una1",
"una"
]
]
],
[
"synCat",
[
[
"lexClass",
"undefArticle"
]
]
],
[
"lexId",
"un"
]
],
[
"divergenza1",
[
"footprints",
[
"synXGiveRiseToYCxn"
]
],
[
"parent",
"comportare1"
],
[
"dependents",
[
"una1",
"radicale1"
]
],
[
"form",
[
[
"string",
"divergenza1",
"divergenza"
]
]
],
[
"dependency",
[
[
"posTag",
"s__gender=fem|number=sing"
],
[
"edge",
"obj"
]
]
],
[
"referent",
"?effect3"
]
],
[
"radicale1",
[
"parent",
"divergenza1"
],
[
"dependents",
null
],
[
"form",
[
[
"string",
"radicale1",
"radicale"
]
]
],
[
"dependency",
[
[
"posTag",
"a__number=sing"
],
[
"edge",
"amod"
]
]
]
],
[
"nella1",
[
"dependency",
[
[
"posTag",
"a__gender=fem|number=sing"
],
[
"edge",
"amod"
]
]
],
[
"dependents",
null
],
[
"parent",
"distribuzione1"
],
[
"form",
[
[
"string",
"nella1",
"nella"
]
]
],
[
"synCat",
[
[
"lexClass",
"preposition"
]
]
],
[
"lexId",
"nel"
]
],
[
"distribuzione1",
[
"parent",
"comportare1"
],
[
"dependents",
[
"nella1",
"capitale1"
]
],
[
"form",
[
[
"string",
"distribuzione1",
"distribuzione"
]
]
],
[
"dependency",
[
[
"posTag",
"s__gender=fem|number=sing"
],
[
"edge",
"nsubj"
]
]
]
],
[
"del2",
[
"dependency",
[
[
"posTag",
"e__gender=masc|number=sing"
],
[
"edge",
"det"
]
]
],
[
"dependents",
null
],
[
"parent",
"capitale1"
],
[
"form",
[
[
"string",
"del2",
"del"
]
]
],
[
"synCat",
[
[
"lexClass",
"preposition"
]
]
],
[
"lexId",
"di"
]
],
[
"capitale1",
[
"parent",
"distribuzione1"
],
[
"dependents",
[
"del2"
]
],
[
"form",
[
[
"string",
"capitale1",
"capitale"
]
]
],
[
"dependency",
[
[
"posTag",
"s__gender=masc|number=sing"
],
[
"edge",
"amod"
]
]
]
],
[
".1",
[
"parent",
"nota1"
],
[
"dependents",
null
],
[
"form",
[
[
"string",
".1",
"."
]
]
],
[
"dependency",
[
[
"posTag",
"fs___"
],
[
"edge",
"punct"
]
]
]
]
]发布于 2021-08-11 17:59:15
我为你创建了一个沙箱。我想这就是你需要的。我的解决方案建议是过滤器和正则表达式的组合。
codesandbox:
https://codesandbox.io/s/amazing-mendeleev-lotfk?file=/src/index.js
解决方案的代码:
var regex= /cause/; // meaning is contains 'cause' and starts or ends with any other characters
console.log([your_array].filter((x) => regex.exec(x))
);注意:您可以在您的filterIt函数中使用相同的正则表达式,但它仍可能返回1条记录。
https://stackoverflow.com/questions/68744816
复制相似问题