从JavaScript中的搜索中获取多个JSON值

huangapple go评论83阅读模式
英文:

Get multiple JSON values from search in JS

问题

  1. console.log(event.pos[0]['$'].a, event.pos[0]['$'].x, event.pos[0]['$'].z);
英文:

I need to loop thru json data looking for a keyword. Once found I need to pull 3 values to my console.log

This is for a Node.js project. The json is a local file.

How the data is laid out.

  1. {
  2. "eventposdef": {
  3. "event": [
  4. {
  5. "$": {
  6. "name": "FIND THIS"
  7. },
  8. "pos": [
  9. {
  10. "$": {
  11. "a": "THIS VALUE 1",
  12. "x": "THIS VALUE 2",
  13. "z": "THIS VALUE 3"
  14. }
  15. },
  16. ]
  17. }

Ive tried the following but it only returns the name.

  1. // result is the entire file loaded. The below code does get part of the inquiry
  2. var json = result;
  3. json.eventposdef.event.forEach(event => {
  4. if (event["$"].name === var2){
  5. console.log(event.name) //returns the name
  6. console.log(event.pos) // returns nothing
  7. console.log(event.pos.a) //returns nothing
  8. }
  9. })

I expect to return...

  1. "a" THIS VALUE 1
  2. "x" THIS VALUE 2
  3. "z" THIS VALUE 3

答案1

得分: 0

我不确定您是要按事件名称筛选还是打印所有位置,但您可以使用Object.keys(pos.$)来获取对象的所有属性并将它们打印到控制台上。这是我的参考代码,我跳过了null和undefined的检查条件。

  1. const data = {
  2. "eventposdef": {
  3. "event": [
  4. {
  5. "$": {
  6. "name": "FIND THIS"
  7. },
  8. "pos": [
  9. {
  10. "$": {
  11. "a": "THIS VALUE 1",
  12. "x": "THIS VALUE 2",
  13. "z": "THIS VALUE 3"
  14. }
  15. },
  16. ]
  17. }
  18. ]
  19. }
  20. };
  21. // 遍历事件
  22. data.eventposdef.event.forEach(event => {
  23. // 检查事件名称是否是我们想要的
  24. if (event.$.name !== 'FIND THIS') {
  25. return;
  26. }
  27. event.pos.forEach(pos => {
  28. Object.keys(pos.$).forEach(key => {
  29. console.log(`"${key}" ${pos.$[key]}`);
  30. });
  31. });
  32. });

这段代码会遍历事件,检查事件名称是否为“FIND THIS”,然后遍历位置属性并将它们打印到控制台上。

英文:

I'm not sure if you want to filter by event name or print all pos, but you can use Object.keys(pos.$) to get all the properties of an object and print them to the console. This is my reference code, I skipped null and undefined check conditions.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const data = {
  2. &quot;eventposdef&quot;: {
  3. &quot;event&quot;: [
  4. {
  5. &quot;$&quot;: {
  6. &quot;name&quot;: &quot;FIND THIS&quot;
  7. },
  8. &quot;pos&quot;: [
  9. {
  10. &quot;$&quot;: {
  11. &quot;a&quot;: &quot;THIS VALUE 1&quot;,
  12. &quot;x&quot;: &quot;THIS VALUE 2&quot;,
  13. &quot;z&quot;: &quot;THIS VALUE 3&quot;
  14. }
  15. },
  16. ]
  17. }]}};
  18. // Loop through the events
  19. data.eventposdef.event.forEach(event =&gt; {
  20. // Check if the event name is the one we want
  21. if (event.$.name !== &#39;FIND THIS&#39;) {
  22. return;
  23. }
  24. event.pos.forEach(pos =&gt; {
  25. Object.keys(pos.$).forEach(key =&gt; {
  26. console.log(`&quot;${key}&quot; ${pos.$[key]}`);
  27. });
  28. });
  29. });

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 10:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定