如何进入从json获取的数组?

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

我有一个 JSON 文件。我正在使用 fetch 从该文件中提取数据。但我想获取我拉出的数组的所有元素中的第一个元素。我尝试过这个但找不到它。你能帮我吗?

我的 JSON 数据:

{
    "cols": [
        "Name Surname",
        "Company",
        "Email",
        "Date",
        "Country",
        "City"
    ],
    "data": [
        [
            "Hyacinth Vincent",
            "Duis Corporation",
            "[email protected]",
            "28/06/2022",
            "Eritrea",
            "Lyubertsy"
        ],
        [
            "Brenden Martinez",
            "Volutpat Nunc Associates",
            "[email protected]",
            "24/03/2021",
            "British Indian Ocean Territory",
            "Colwood"
        ],
        [
            "Lunea Kinney",
            "Ornare Facilisis Eget Incorporated",
            "[email protected]",
            "20/10/2021",
            "Jordan",
            "Yorkton"
        ],
        [
            "Rhona Mooney",
            "Sed Et PC",
            "[email protected]",
            "10/11/2020",
            "Azerbaijan",
            "Shrewsbury"
        ]
    ]
}

我的 JS 代码:

fetch("api/tesodev.json")
  .then((response) => response.json())
  .then((data) => {
    let cols = data.cols;
    let responseData = data.data;
  for (let index = 0; index < responseData.length; index++) {
    let newResponseData = responseData.map(function (el, i) {
        console.log(el[i]);
      });
      
  }
    console.log(cols[0]);
  });

我想要做的正是获取数据数组所有元素的第一个元素。 我想访问“Hyacinth Vincent”、“Brenden Martinez”、“Lunea Kinney”、“Rhona Mooney”的数据。

javascript fetch-api
5个回答
2
投票

你做错的唯一一件事是映射整个子数组并记录它的每个部分,当你想要的是第一个元素时:

const data = {
  "cols": [
    "Name Surname",
    "Company",
    "Email",
    "Date",
    "Country",
    "City"
  ],
  "data": [
    [
      "Hyacinth Vincent",
      "Duis Corporation",
      "[email protected]",
      "28/06/2022",
      "Eritrea",
      "Lyubertsy"
    ],
    [
      "Brenden Martinez",
      "Volutpat Nunc Associates",
      "[email protected]",
      "24/03/2021",
      "British Indian Ocean Territory",
      "Colwood"
    ],
    [
      "Lunea Kinney",
      "Ornare Facilisis Eget Incorporated",
      "[email protected]",
      "20/10/2021",
      "Jordan",
      "Yorkton"
    ],
    [
      "Rhona Mooney",
      "Sed Et PC",
      "[email protected]",
      "10/11/2020",
      "Azerbaijan",
      "Shrewsbury"
    ]
  ]
}

let cols = data.cols;
let responseData = data.data;
for (let index = 0; index < responseData.length; index++) {
  console.log(responseData[index][0])
}

(我强烈建议您更改数据格式;对象数组不会那么脆弱:

    ...
    "data": [
        {
            name: "Hyacinth Vincent",
            company: "Duis Corporation",
            email: "[email protected]",
            date: "28/06/2022",
            country: "Eritrea",
            city: "Lyubertsy"
        },
        {
          ...

2
投票

嗯...

我认为这其中存在多个问题 newResponseData 将是具有未定义值的项目数组,因为 console.log() 返回未定义。

我认为这两行可以有所帮助。 你可以检查一下吗

 let responseData = data.data;
  const finaldata = responseData.map(item => item [0])


1
投票

您需要访问 for 循环中的当前行,然后访问该行的第一个元素。

fetch("api/tesodev.json")
  .then((response) => response.json())
  .then((data) => {
    let cols = data.cols;
    let responseData = data.data;
    for (let index = 0; index < responseData.length; index++) {
      const myDataRow = responseData[index];
      const theFirstDataOfMyRow = myDataRow[0];
      console.log(theFirstDataOfMyRow); 
    }
    console.log(cols[0]);
  });

1
投票

您可以使用双索引来访问第一个字段。

[i][0]
将访问第
i
第 个对象中的第一个字段,

let responseData = <json>;

for (let index = 0; index < responseData.length; index++) {
    console.log(responseData[i][0]);
}

1
投票

您几乎已经按照您希望的方式进行了。

const data = {
    "cols": [
        "Name Surname",
        "Company",
        "Email",
        "Date",
        "Country",
        "City"
    ],
    "data": [
        [
            "Hyacinth Vincent",
            "Duis Corporation",
            "[email protected]",
            "28/06/2022",
            "Eritrea",
            "Lyubertsy"
        ],
        [
            "Brenden Martinez",
            "Volutpat Nunc Associates",
            "[email protected]",
            "24/03/2021",
            "British Indian Ocean Territory",
            "Colwood"
        ],
        [
            "Lunea Kinney",
            "Ornare Facilisis Eget Incorporated",
            "[email protected]",
            "20/10/2021",
            "Jordan",
            "Yorkton"
        ],
        [
            "Rhona Mooney",
            "Sed Et PC",
            "[email protected]",
            "10/11/2020",
            "Azerbaijan",
            "Shrewsbury"
        ]
    ]
}

// Simplyfied, get data, use only data, only putput first entry in the array.
data.data.forEach((person) => {
  console.log(person[0])
})

// Would be something like this:
/*
fetch("api/tesodev.json")
  .then((response) => response.json())
  .then((data) => {
    let cols = data.cols
    let persons = data.data
    for (i = 0; i < persons.length; i++) {
      console.log(persons[i][0])
    }
  })
*/

© www.soinside.com 2019 - 2024. All rights reserved.