skip to Main Content

I have a JSON array having hundreds of objects where each JSON object having name and hobbies property.

Below is the JSON structure:

 const data = [
 {
  name:'Paul',
  hobbies: ['Football','Reading']
 },
 {
  name:'Riya',
  hobbies: ['Singing','Dancing']
 },
 {
  name:'Jack',
  hobbies: ['Gaming']
 }
]

So here if I will iterate through this data it will give me same name multiple times wherever multiple hobbies are present.So if I am console it result would be

Paul,Football
Paul,Reading
Riya,Singing
Riya,Dancing
Jack,Gaming  

I don’t want above output I want wherever there is same name is coming in a same object don’t console it like below:

Paul,Football
"",Reading
Riya,Singing
"",Dancing
Jack,Gaming  

Below is my code:

  const data = [
  {
   name:'Paul',
   hobbies: ['Football','Reading']
  },
  {
   name:'Riya',
   hobbies: ['Singing','Dancing']
  },
  {
   name:'Jack',
   hobbies: ['Gaming']
  }
 ]

const example = (data) => {

for(var i=0;i<data.length;i++){

    for(var j=0;j<data[i].hobbies.length;j++){

        console.log(data[i].name,data[i].hobbies[j]);

         if(i=0){
           console.log(data[i].name,data[i].reports[j]);
        }
        else{
            const prev = i-1;
            if(data[prev].name == data[i].name) { //Getting TypeError here cannot read property 'name' of undefined 
               console.log("",data[i].reports[j]);
            }
            else{
               console.log(data[i].name,data[i].reports[j]); 
            }
        }
      }
    }
 }

example(data);

In above code I am trying to compare the previous value of name in data array with the current value of name. If it’s same then making name field " " else putting name value and for first element for position 0 I am putting value as it is.

Why am I getting this TypeError?

2

Answers


  1. const data = [
     {
      name:'Paul',
      hobbies: ['Football','Reading']
     },
     {
      name:'Riya',
      hobbies: ['Singing','Dancing']
     },
     {
      name:'Jack',
      hobbies: ['Gaming']
     }
    ]
    
    data.forEach(d => {
      d.hobbies.forEach((hobby, index) => {
        const name = index == 0 ? d.name : '""'
        console.log(name + ',' + hobby)
      })
    })

    Just print the name if index of hobby is 0

    Login or Signup to reply.
  2. There are several issues, first is typo, you assigned instead of comparing

    if (i=0) {
    // ^^^^^
      console.log(data[i].name,data[i].reports[j]);
    }
    

    The rest is logic, all you have to do is to check the index of j

    const example = data => {
      for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < data[i].hobbies.length; j++) {
          if (j == 0) {
            console.log(data[i].name, data[i].hobbies[j])
          } else {
            console.log("", data[i].hobbies[j])
          }
        }
      }
    }
    

    Full solution

    const data = [
      {
        name: "Paul",
        hobbies: ["Football", "Reading"],
      },
      {
        name: "Riya",
        hobbies: ["Singing", "Dancing"],
      },
      {
        name: "Jack",
        hobbies: ["Gaming"],
      },
    ]
    
    const example = data => {
      for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < data[i].hobbies.length; j++) {
          if (j == 0) {
            console.log(data[i].name, data[i].hobbies[j])
          } else {
            console.log("", data[i].hobbies[j])
          }
        }
      }
    }
    
    example(data)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search