简体   繁体   English

如何从本机中的另一个数组中获取数组值?

[英]How to get array value from the other array in react native?

In this code I want the value in centreValues to come from some other array 在这段代码中,我希望centerValues中的值来自其他数组

    const centreValues=[
  {
    title:'Type',
    value:centreDetail.location_type
  },
  {
    title:'Address',
    value:centreDetail.address1+centreDetail.address2
  },
  {
    title:'City',
    value:centreDetail.city
  },
  {
    title:'State/Province',
    value:centreDetail.state
  },
  {
    title:'Postal/Zipcode',
    value:centreDetail.zip
  },
  {
    title:'Phone',
    value:centreDetail.phone
  },
]

my centreDetails is json like this: 我的centerDetails是这样的json:

centreDetails={
   location_type:'some Value',
   address1: 'some Value',
   address2:'some Value',
   ....
}

I want to bring these values in centreValues array.How could I do that? 我想将这些值带入centerValues数组中,该怎么办?

to get all values of centreDetail as object result 获取centerDetail的所有值作为对象结果

centreValues.map(x=>x.value.centreDetail)  

To transfer to another array, manage the state like this: 要转移到另一个阵列,请按以下方式管理状态:

centreValues.map(x=> this.setState({ centreDetailArray: [...this.state.centreDetailArray, x.value.centreDetail] })

Then you can get the result from state like this: 然后,您可以从如下状态获取结果:

<View>  
  {this.state.centreDetailArray.map(x=><Text>{x}</Text>)}
</View>

its a easy JS object and array scenario. 这是一个简单的JS对象和数组方案。 According to your provided array it seems that you expect to have centreDetail . 根据您提供的数组,您似乎希望具有centreDetail see my below example 看我下面的例子

const centreDetail = {
     location_type: "something",
    city: "something",
    state: "something",
    zip: "something",
    phone: "something",
}

now you can call the following above object in your array 现在您可以在数组中调用以下对象

const centreValues=[
  {
    title:'Type',
    value:centreDetail.location_type
  },

  {
    title:'City',
    value:centreDetail.city
  },
  {
    title:'State/Province',
    value:centreDetail.state
  },
  {
    title:'Postal/Zipcode',
    value:centreDetail.zip
  },
  {
    title:'Phone',
    value:centreDetail.phone
  },
]

EDIT: you added json in your question now. 编辑:您现在在问题中添加了json。 therefor, you need a loop. 因此,您需要一个循环。 use for loop or while to go through the each index of array and added in your other array. 使用for循环或while来遍历数组的每个索引并添加到​​其他数组中。 you can also use map for that also 您也可以使用map

EDIT according to your comment. 根据您的评论进行编辑。 are you sure about that. 您确定吗。 see i typed this all in console. 看到我在控制台中输入了所有这些。 it seems to be working. 它似乎正在工作。

在此处输入图片说明

Here is how you can achieve it : 这是您可以实现的方法:

 const centreDetails = { location_type:'my location', address1: 'an address', address2: 'another address', phone: '516546548', city: 'Wakanda' } const centreValues = Object.entries(centreDetails).map(([title, value]) => ({ title, value})) console.log(centreValues) 

You will have to convert your object into an array made out of pairs of key and values which is made using Object.entries() 您将必须将对象转换为由使用Object.entries()构成的键和值对组成的数组

Then you only have and create your desired structure using map on your array 然后,您只有使用数组上的map并创建所需的结构


EDIT 编辑
You can apply an additional filter function if you only want certain fields : 如果只需要某些字段,则可以应用其他过滤器功能:

 const centreDetails = { location_type: 'my location', address1: 'an address', address2: 'another address', phone: '516546548', city: 'Wakanda' } const desiredValues = ["location_type", "address2", "city"] const centreValues = Object.entries(centreDetails) .filter(([title, value]) => !!desiredValues.includes(title)) //Checks if the value exists .map(([title, value]) => ({ title, value})) console.log(centreValues) 


EDIT 2 : 编辑2:

If you want to have a different alias here's a way to do it : 如果您想使用其他别名,可以使用以下方法:

 const centreDetails = { location_type: 'my location', address1: 'an address', address2: 'another address', phone: '516546548', city: 'Wakanda' } const desiredValues = { "location_type" : "location", "address2": "this guy\\'s second address", "city": "Hey, he lives here !" } const centreValues = Object.entries(centreDetails) .filter(([title, value]) => desiredValues[title]) .map(([title, value]) => ({ title : desiredValues[title], value})) console.log(centreValues) 

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

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