Ronny Fitzgerald
Ronny Fitzgerald

Reputation: 131

How to map an array of objects when one of the objects is empty?

I'm basically creating a bootleg amazon clone and I'm working on the Questions & Answers component. In my dummyData, it's possible for a product to have no questions therefore, no answers. This creates a problem for when I map my questions and answers in react. I looked at similar posts regarding this but I believe my situation is a bit different. I tried defaulting to an empty array but that didn't work for me.

DummyData.js
const dummyData = [
{
    productId: 100,
    questions: [{
      questionId: 10,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 500,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 501,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 502,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 503,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    },
    {
      questionId: 11,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 504,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 505,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 506,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 507,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    },
    {
      questionId: 12,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 508,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 509,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 510,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 511,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    },
    {
      questionId: 13,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 512,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 513,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 514,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 515,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    },
    {
      questionId: 14,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 516,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 517,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 518,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 519,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    },
    {
      questionId: 15,
      helpfulCount: 67,
      body: 'Is this watch waterproof?',
      answers: [{
        answerId: 520,
        body: 'This works up to 5 meters underwater.',
        user: 'User231',
        photo: {
          photoBody:'Here are some photos of the waterproof watches:',
          images: ['some link'],
          photoHelpfulCount: 33
        },
        helpfulCount: 43,
        date: 'May 1, 2019',
        isSeller: true,
        isReported: false
    },
    {
      answerId: 521,
      body: `To a degree, I wouldn't test it out.`,
      user: 'User420',
      photo: {
        photoBody:'',
        images: [],
        photoHelpfulCount: -1
      },
      helpfulCount: 0,
      date: 'May 3, 2019',
      isSeller: true,
      isReported: false
    },
    {
      answerId: 522,
      body: 'I tested it out 7ft underwater and so far so good.',
      user: 'User367',
      photo: {
        photoBody:'Here is a photo of my watch underwater:',
        images: ['some link'],
        photoHelpfulCount: 5
      },
      helpfulCount: 8,
      date: 'May 6, 2019',
      isSeller: true,
      isReported: false
    },
    { 
      answerId: 523,
      body: 'Tried it out at a water park and it stopped working.',
      user: 'User514',
      photo: {
        photoBody:'Here are some photos of the aftermath:',
        images: ['some link'],
        photoHelpfulCount: 2
      },
      helpfulCount: 4,
      date: 'May 1, 2019',
      isSeller: true,
      isReported: false
    }],
    }
  ]      
}, // etc. etc.
{ // this creates the problem so I commented it out and mapped every 0 index as you will see
    productId: 105,
    questions: [{}]      
},
];

Questions.jsx
import React from 'react';
import Question from './Question.jsx';
import Answer from './Answer.jsx';

const Questions = ({questionList}) => {

return (
  <div>   
    {questionList.map(productObj => (
    <div key={productObj.productId}>
      <Question 
      questionObj={productObj.questions[0]}
      updatedDataList={questionList}
      /> 
      <Answer 
      answerObj={productObj.questions[0].answers[0]} 
      updatedDataArr={questionList}
      />
    </div>
    ))}  
    {console.log(questionList)} 
  </div>  
  );
};

export default Questions;

index.jsx :
import React, { useState, useEffect } from 'react';
import Questions from './Questions.jsx';
import SearchBar from './SearchBar.jsx';
import AddQuestion from './AddQuestion.jsx';
import MoreAnsweredQuestions from './MoreAnsweredQuestions.jsx';
import dummyData from './dummyData.js';

const QuestionsAndAnswers = () => { 

  const [questions, setQuestions] = useState([]);

  useEffect(() => {
    setQuestions(dummyData)
  }, [])

  const handleSearchQuestion = query => {
    if (query.length < 3) {
      setQuestions(dummyData);
    } else {
      const searchResult = questions.filter(question => question.body.includes(query));
      setQuestions(searchResult);
    }
  };

  return (
    <div>
      <h2>Questions & Answers</h2>
      <SearchBar searchQuestion={(query) => handleSearchQuestion(query)}/>
      <Questions questionList={questions} />
      <MoreAnsweredQuestions />
      <AddQuestion />
    </div>
  );
};

export default QuestionsAndAnswers;

Upvotes: 0

Views: 105

Answers (1)

Chuck
Chuck

Reputation: 804

You gave default value as [{}]

Try to set it as [].

The difference is that [{}] is not an empty array.

Upvotes: 1

Related Questions