0

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?

3
  • Can you make a clearer explanation about : What your input is, what your current output is and what your desired output is Commented Dec 17, 2018 at 10:24
  • see edited version Commented Dec 17, 2018 at 10:31
  • Your code does what you explained you want it to do. Commented Dec 17, 2018 at 10:31

3 Answers 3

1

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.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I am using the data like this only but the value is coming undefined
My structure is similar but I am getting values undefined in console
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {title: "Type", value: undefined} 1: {title: "Address", value: NaN} 2: {title: "City", value: undefined} 3: {title: "State/Province", value: undefined} 4: {title: "Postal/Zipcode", value: undefined} 5: {title: "Phone", value: undefined} 6: {title: "Fax", value: undefined} 7: {title: "Email", value: undefined} 8: {title: "President", value: undefined} 9: {title: "Secretary", value: undefined} 10: {title: "Treasurer", value: undefined}
@EshantBist can you try in console by copying your both arrays?
1

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)

4 Comments

What if I want only some elements of this array?
You can filter out the array created, i'll update it with a new snippet
and suppose I want to change that title location_type as location what should I do for it
Alright, I'll update my answer in a few minutes, try listing everything you need in your question at once
0

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>

3 Comments

This is not a React question ;)
It is written 'React Native' in your title
Its Basically a react question doesn't matter whether its react or react-native

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.