Home > Software design >  How do I get into the array I got from json?
How do I get into the array I got from json?

Time:09-27

I have a JSON file. I'm pulling data from this file with fetch. But I want to get the first element in all the elements of the array I pulled. I tried this but couldn't find it. Can you help me ?

MY JSON 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"
        ]
    ]
}

MY JS CODE :

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]);
  });

Exactly what I'm trying to do is to get to the first elements of all the elements of the data array. I want to access the data of "Hyacinth Vincent", "Brenden Martinez", "Lunea Kinney", "Rhona Mooney".

CodePudding user response:

The only thing you're doing wrong is mapping over the whole child array and logging every part of it, when what you want is the first element:

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])
}

(I would strongly suggest you change your data format; an array of objects would be much less fragile:

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

CodePudding user response:

You can use a double index to access the first field. [i][0] will access the first field in the ith object,

let responseData = <json>;

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

CodePudding user response:

You need to access the current row in your for loop, then access the first element of this row.

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]);
  });

CodePudding user response:

You're almost doing it the way you want it work.

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])
    }
  })
*/

CodePudding user response:

ummm...

I think there are multiple problems in this newResponseData will be array of item having values undefined as console.log() returns undefined.

I think this two lines can help. Can you check at your end

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

  • Related