2

I have the following data stored in the Async Storage in react native. Can anyone please help me to parse the data from it.

  1. Need to display name array alone
  2. Need to parse the name array items by looping through it

    { "name": [{ "name": "Ford", "models": "Fiesta" }, { "name": "BMW", "models": "320" }, { "name": "Fiat", "models": "500" } ] }

2 Answers 2

1

You could use JSON.parse() and then get the data you want like so:

const obj = JSON.parse(yourObj)

const { name } = obj
console.log(name)
name.forEach(elem => {
  console.log(elem)
  //do whatever you want with each elem
})
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to parse using for loop for the same?
1

Problem: AsyncStorage getItem returns string

Solution: When you getItem from AsyncStorage it returns string. So you will get your data as string parse it using JSON.parse and map to get an array of names. Like

In case of AsyncStorage you need to store it in string format and it will return in string format. while storing use JSON.stringify() to store either object or array. Now to access that Async value get using getItem and parse it using JSON.parse()

let arr = { "name": [{ "name": "Ford", "models": "Fiesta" }, { "name": "BMW", "models": 320 }, {"name": "Fiat", "models": 500 } ] }
const nameArray = arr.name.map(m=>m.name)
console.log(nameArray)
const nameObjArray = arr.name.map((m)=>{return {"name":m.name}})
console.log(nameObjArray)

4 Comments

I stored the same json in the async storage and accessing it and tried assigning the value directly to the arr here. then it is not working. can u tell me any work around for this
because you JSON not holding arr directly. you need to access by using name which you key
I am assigning entire value directly to the arr here. So ideally it should work ryt? because both arr and value from the storage are same
in your case, you put array in object with key as name but if you want to save array directly in async then use JSON.stringify(arr) to store and to get async parse if and use it

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.