简体   繁体   中英

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

    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:

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?

to get all values of centreDetail as object result

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. According to your provided array it seems that you expect to have 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. therefor, you need a loop. use for loop or while to go through the each index of array and added in your other array. you can also use map for that also

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

Then you only have and create your desired structure using map on your array


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 :

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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