簡體   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