1

I'm able to call parcels using map as list.parcels and the output comes like

{parcelId: "12"}
{parcelId: "14"}
{parcelId: "15"}
{parcelId: "16"}
{parcelId: "17"}

but i need to print only parcelId value, so to do that I tried list.parcels.parcelId and it shows undefined. I need help in printing the parcelId alone. Thanks in advance.

Json is as below:

{
      "id": "7",
      "type": "history",
      "title": "History Parcel Delivery",
      "sender": "Paul",
      "price": "635.50",
      "parcels": [
        {
          "parcelId": "12"
        },
        {
          "parcelId": "14"
        },
        {
          "parcelId": "15"
        },
        {
          "parcelId": "16"
        },
        {
          "parcelId": "17"
        }
      ]
    }

2 Answers 2

2

Since, list.parcels is an array you must use an index to reference an object within it. So to refer to the first item in the array you should use list.parcels[0].parcelId. This will print "12". So to print a specific parcelId use:

list.parcels[i].parcelId

where value of i is between 0 and one less than the length of the array

If you just need a list of the parcelId you can use

parcelIds = lists.parcels.map((parcel) => parcel.parcelId);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. That answered my question :-)
Now I got the answer like 1214151617, is there a way to print the number one by one so that i can set an icon to each parcel id. Output like: Parcel Id : 12 (Icon) Parcel Id: 14 (Icon) and so on
I am not 100% sure what you mean but you can do something like parcelIds = lists.parcels.map((parcel) => `${parcel.parcelId} (Icon)`);
Thank you, Now the output is ParcelID: 1214151617 I need out put like ParcelID : 12 Parcel ID: 14 Parcel ID: 15 Parcel ID: 16 Parcel ID: 17 I mean printing values one after one and not continuously
To do that why don't you just use a for loop?
|
0

i know it to much late,but i hope its helpfull for others,How to Accessing Json Array of objects in React-Native

 {
    "name":"John",
    "age":30,
    "cars":[ "Ford", "BMW", "Fiat" ]
    }



var cardatafetch=[]
for (i in myObj.cars) {
  var dataa= myObj.cars[i];

//and for push the array data into array var,i declare it above the for loop you can declare any where 

 cardatafetch.push(dataa)

}
console.log(cardatafetch)

Comments

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.