6

Am actively creating a react native app for my college studies ! In that am using external api as data source. The problem am facing is that sometimes am getting data in single object and sometimes its in array format. How to handle this in react native perspective ?

As am maping jsx considering it as array but when i get object it throws error

My data example:

const data = {         // this is example of single object 
   entry: {
      key: "0",
      value: "Patrik"
   }
}

const data = {         // this is example of array
   entry: [
   {
      key: "0",
      value: "Patrik"
   },
    {
      key: "1",
      value: "John"
   }],
}

So this is how i get data from external api, and now my react native jsx code:

     { data.entry.map(o => {
               <View key={o.key}>
                      <View style={{ paddingLeft: 25 }}>
                            <Text style={{ fontWeight: 'bold' }}>{o.value}</Text>
                      </View>
                  </View>

                }) 
      }

So, when i get array, it works but when i get object this throws error ! Any help on this ?

5 Answers 5

1

You should do Array.isArray(data.entry) to check if you have an array of object or a single object. And perform the logic based on that.

Sign up to request clarification or add additional context in comments.

2 Comments

So, if its object how to render jsx ?
If its a single object, you can just do data.entry.value and get the value.
1

Convert Object to Array so you won't get error.

if(!Array.isArray(data.entry)){
  data = { entry: [data.entry] };
}

3 Comments

[data] or [data.entry]?
If so, how to render jsx ?
Updated the answer. it was my mistake. You don't need to change your jsx.
0

Like the above comment says, you should use Array.isArray to check if the data is an array. If its an object something like this should work:

let element = null
if(Array.isArray(data.entry)){
  element = data.entry.map(o => {
     <View key={o.key}>
        <View style={{ paddingLeft: 25 }}>
          <Text style={{ fontWeight: 'bold' }}>{o.value}</Text>
        </View>
     </View>
   }) 
  }
  else{
    element = (
    <View key={data.entry.key}>
        <View style={{ paddingLeft: 25 }}>
          <Text style={{ fontWeight: 'bold' }}>{data.entry.value}</Text>
        </View>
    </View>
  )
}

Comments

0

This might help -- (Updated)

{
  const newData = []; // define empty array
  Array.isArray(data.entry) ? newData.concat(data.entry) : 
                              newData.push(data.entry);
    newData.entry.map((o) => {
      <View key={o.key}>
        <View style={{ paddingLeft: 25 }}>
          <Text style={{ fontWeight: "bold" }}>{o.value}</Text>
        </View>
      </View>;
    })
}

3 Comments

I understand this, but is this is the real way to approach this ? As it extends the length of code right ?
@PatriciaD19 It'd be better to set the value to map over first, so you don't have to copy paste all the JSX. e.g. dataToRender = Array.isArray(data.entry) ? data.entry : [data.entry];. Then just go ahead knowing it's an array data.entry.map... and you don't have to copy/paste JSX for both cases
@PatriciaD19 check updated code which reduces lines of code
0

//jsx

const convertedData =  Object.values(Array.isArray(data.entry)? data.entry: [data.entry]);

Now you can iterate convertedData . But I will suggest instead of using map , please use Flatlist.
If you want to use map then please do null check for data.entry and return the jsx elements.

3 Comments

Whats the advantages of using flatlist instead of map ?
.Quick Rendering .good Performance .Header support .Footer support .Pull to refresh support .Separator support .Scroll loading support .ScrollToIndex support .Cross-Platform support .Configurable callbacks .Optional support for horizontal mode
I believe there wont be any performance issue between using map and flat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.