首页 > 解决方案 > 通过键检索 json 数据

问题描述

  const username = 'merMan';

  fetch("./datz.json")
    .then(response => response.text())
    .then((response) => {
        console.log(response);

    })

我的数据response如下所示,仍然很难简单地获取用户特定的数据。我的response数据输出如下。我试过使用find,但总是返回 find is not a function,response[username]也不起作用。

[{"mermAn":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"},
{"catWoman":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"},
{"Riddler":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"}}]

标签: javascriptarraysjsonfetch

解决方案


使用.json()代替.text()

const username = 'merMan';

fetch("./datz.json")
  .then(response => response.json())
  .then((users) => {
      console.log(users.find(x => typeof x[username] !== "undefined"));
  })

推荐阅读