我有一个 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”的数据。
你做错的唯一一件事是映射整个子数组并记录它的每个部分,当你想要的是第一个元素时:
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"
},
{
...
嗯...
我认为这其中存在多个问题 newResponseData 将是具有未定义值的项目数组,因为 console.log() 返回未定义。
我认为这两行可以有所帮助。 你可以检查一下吗
let responseData = data.data;
const finaldata = responseData.map(item => item [0])
您需要访问 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]);
});
您可以使用双索引来访问第一个字段。
[i][0]
将访问第 i
第 个对象中的第一个字段,
let responseData = <json>;
for (let index = 0; index < responseData.length; index++) {
console.log(responseData[i][0]);
}
您几乎已经按照您希望的方式进行了。
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])
}
})
*/