简体   繁体   English

如何在本机反应中处理 object 和 Array?

[英]How to handle object and Array in react native?

Am actively creating a react native app for my college studies.我正在为我的大学学习积极创建一个反应原生应用程序。 In that am using external api as data source.在那使用外部 api 作为数据源。 The problem am facing is that sometimes am getting data in single object and sometimes its in array format?面临的问题是有时会以单个 object 获取数据,有时以数组格式获取数据? How to handle this in react native perspective ?如何以反应原生的角度处理这个问题?

As am maping jsx considering it as array but when i get object it throws error在映射 jsx 时,将其视为数组,但是当我得到 object 时,它会引发错误

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:所以这就是我从外部 api 获取数据的方式,现在我反应原生 jsx 代码:

     { 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?所以,当我得到数组时,它可以工作,但是当我得到 object 这会引发错误? Any help on this ?对此有什么帮助吗?

Convert Object to Array so you won't get error.将 Object 转换为数组,这样就不会出错。

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

You should do Array.isArray(data.entry) to check if you have an array of object or a single object.您应该执行Array.isArray(data.entry)来检查您是否有一个 object 数组或单个 object 数组。 And perform the logic based on that.并基于此执行逻辑。

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

Like the above comment says, you should use Array.isArray to check if the data is an array.就像上面的评论所说,你应该使用Array.isArray来检查数据是否是一个数组。 If its an object something like this should work:如果它是一个 object ,这样的东西应该可以工作:

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

//jsx //jsx

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

Now you can iterate convertedData.现在您可以迭代convertedData。 But I will suggest instead of using map, please use Flatlist.但我建议不要使用 map,请使用 Flatlist。
If you want to use map then please do null check for data.entry and return the jsx elements.如果您想使用 map 那么请执行 null 检查 data.entry 并返回 jsx 元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM