繁体   English   中英

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

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

在这段代码中,我希望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
  },
]

我的centerDetails是这样的json:

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

我想将这些值带入centerValues数组中,该怎么办?

获取centerDetail的所有值作为对象结果

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

要转移到另一个阵列,请按以下方式管理状态:

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

然后,您可以从如下状态获取结果:

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

这是一个简单的JS对象和数组方案。 根据您提供的数组,您似乎希望具有centreDetail 看我下面的例子

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

现在您可以在数组中调用以下对象

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
  },
]

编辑:您现在在问题中添加了json。 因此,您需要一个循环。 使用for循环或while来遍历数组的每个索引并添加到​​其他数组中。 您也可以使用map

根据您的评论进行编辑。 您确定吗。 看到我在控制台中输入了所有这些。 它似乎正在工作。

在此处输入图片说明

这是您可以实现的方法:

 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) 

您将必须将对象转换为由使用Object.entries()构成的键和值对组成的数组

然后,您只有使用数组上的map并创建所需的结构


编辑
如果只需要某些字段,则可以应用其他过滤器功能:

 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) 


编辑2:

如果您想使用其他别名,可以使用以下方法:

 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