为什么只显示最后一个元素而不是JavaScript中的所有元素

问题描述 投票:0回答:3

我正在尝试从json数据中检索某些信息,并希望创建一个新的键值对数组。但是它只返回最后一个元素而不是所有元素。

我的代码如下:

const input = 
{
  "file1": {
    "function1": {
      "calls": {
        "105": {
          "file": "file1",
          "function": "function2"
        },
        "106": {
          "file": "file1",
          "function": "function3"
        }
      },
      "points": {
        "106": "106"
      }
    },
    "function2": {
      "calls": {
        "109": {
          "file": "file1",
          "function": "function2"
        }
      },
      "points": {
        "109": "111"
      }
    },
    "function3": {
      "calls": {},
      "points": {
        "132": "135"
      }
    }
  }
}

function transformData(input) {
  let  res = [];
  Object.entries(input).map(([fileName, fileObject]) => {
    Object.entries(fileObject).map(([functionName, functionObject]) => {
      Object.entries(functionObject).map(([functionKey, functionValue]) => {
        if(functionKey === "calls") {
          Object.entries(functionValue).map(([callKey, callObject]) => {
            res = {"source": functionName, "target": callObject['function']}
            //console.log(res); // here all elements get printed out
          });
        }   
      });
    });
   });
  return res;
 }

 const result = transformData(input);
 console.log(result) // only giving {source:"function2", target:"function2"}

因此,我想要新的源,目标对,其中源是文件(功能1,功能2)下的键。目标是键“调用”(功能2,功能3,功能2)内嵌套键“功能”的值。在这里文件和功能的数量将会更多。但是某些功能可能根本没有“调用”数据。因此,结果将如下所示:

[
  {
    source: "function1",
    target: "function2"
  },
  {
    source: "function1",
    target: "function3"
  },
  {
    source: "function2",
    target: "function2"
  }
]

谁能帮我获得正确的输出。谢谢您的时间。

javascript arrays json object mapping
3个回答
0
投票

看起来像res =的东西必须像res +=的东西,即不要每次都覆盖数组,而要添加找到的下一个项目。


0
投票

我不确定您的对象结构有多“保证”,但是假设您要遍历所有file*键并获取函数映射,这应该可以解决问题。

const input = 
{
  "file1": {
    "function1": {
      "calls": {
        "105": {
          "file": "file1",
          "function": "function2"
        },
        "106": {
          "file": "file1",
          "function": "function3"
        }
      },
      "points": {
        "106": "106"
      }
    },
    "function2": {
      "calls": {
        "109": {
          "file": "file1",
          "function": "function2"
        }
      },
      "points": {
        "109": "111"
      }
    },
    "function3": {
      "calls": {},
      "points": {
        "132": "135"
      }
    }
  }
}

const result = [];

for(const key in input) {
  if (key.includes('file')) {
    const functions = Object.keys(input[key]);
    for (const func of functions) {
      const funcObject = input[key][func];
      for (const call in funcObject.calls) {
        const callObj = funcObject.calls[call];
        result.push({source: func, target: callObj.function});
      }
    }
  }
}
console.log(result);

0
投票

您需要按如下方式编辑一行

 res = [...res,{"source": functionName, "target": callObject['function']}]
© www.soinside.com 2019 - 2024. All rights reserved.