0

I have a JSON file and I am trying to understand how it works in JavaScript to manipulate it.

here is my JSON:

class UserProfilesData {
    static Profiles = [{
        "id": "u0000000001",
        "name": "Jane Doe",
        "profilePic":"https://cdn.pixabay.com/photo/2015/03/03/08/55/portrait-657116_960_720.jpg",
        "profileHeaderPic":"https://cdn.pixabay.com/photo/2019/10/29/14/35/candle-4587072_960_720.jpg",
        "bio": "I am super nice and energetic. love people and sharing my knowledge",
        "credits": "23",
        "address": {
            "address1": "123 lois lane",
            "address2": "",
            "city": "gotham",
            "county": "Orange County",
            "zip": "92604",
            "state": "California",
            "country": "United States"
        },
        "pics": ["https://cdn.pixabay.com/photo/2020/07/14/03/07/sparrow-5402638_1280.jpg", 
        "https://cdn.pixabay.com/photo/2017/03/13/10/25/hummingbird-2139279_1280.jpg",
        "https://cdn.pixabay.com/photo/2020/07/10/08/07/sunflower-5389923_1280.jpg",
        "https://cdn.pixabay.com/photo/2014/04/14/20/11/japanese-cherry-trees-324175_1280.jpg"
    ],
        "classEnrolled": [{}],
        "classCreated":[{}],
        "hosting":[{}],
        "bookmark":[{}],
        "reviews":[{ "name": "Gabby Caldwell",
        "rate": "3",
        "total_review": "13",
        "text": "Rachel was such a kind and knowledgeable guide. She took us to see some hidden coves that a lot of tourists probabaly miss. I’m keeping the map I made FOREVER!!!"}]
    },
    {
        "id": "u0000000002",
        "name": "Jane Doe",
        "profilePic":"https://cdn.pixabay.com/photo/2015/03/03/08/55/portrait-657116_960_720.jpg",
        "profileHeaderPic":"https://cdn.pixabay.com/photo/2019/10/29/14/35/candle-4587072_960_720.jpg",
        "bio": "I am super nice and energetic. love people and sharing my knowledge",
        "credits": "23",
        "address": {
            "address1": "123 lois lane",
            "address2": "",
            "city": "gotham",
            "county": "Orange County",
            "zip": "92604",
            "state": "California",
            "country": "United States"
        },
        "pics": ["https://cdn.pixabay.com/photo/2020/07/14/03/07/sparrow-5402638_1280.jpg", 
        "https://cdn.pixabay.com/photo/2017/03/13/10/25/hummingbird-2139279_1280.jpg",
        "https://cdn.pixabay.com/photo/2020/07/10/08/07/sunflower-5389923_1280.jpg",
        "https://cdn.pixabay.com/photo/2014/04/14/20/11/japanese-cherry-trees-324175_1280.jpg"
    ],
        "classEnrolled": [{}],
        "classCreated":[{}],
        "hosting":[{}],
        "bookmark":[{}],
        "reviews":[{ "name": "Gabby Caldwell",
        "rate": "3",
        "total_review": "13",
        "text": "Rachel was such a kind and knowledgeable guide. She took us to see some hidden coves that a lot of tourists probabaly miss. I’m keeping the map I made FOREVER!!!"}]
    },
    {
        "id": "u0000000003",
        "name": "Jane Doe",
        "profilePic":"https://cdn.pixabay.com/photo/2015/03/03/08/55/portrait-657116_960_720.jpg",
        "profileHeaderPic":"https://cdn.pixabay.com/photo/2019/10/29/14/35/candle-4587072_960_720.jpg",
        "bio": "I am super nice and energetic. love people and sharing my knowledge",
        "credits": "23",
        "address": {
            "address1": "123 lois lane",
            "address2": "",
            "city": "gotham",
            "county": "Orange County",
            "zip": "92604",
            "state": "California",
            "country": "United States"
        },
        "pics": ["https://cdn.pixabay.com/photo/2020/07/14/03/07/sparrow-5402638_1280.jpg", 
        "https://cdn.pixabay.com/photo/2017/03/13/10/25/hummingbird-2139279_1280.jpg",
        "https://cdn.pixabay.com/photo/2020/07/10/08/07/sunflower-5389923_1280.jpg",
        "https://cdn.pixabay.com/photo/2014/04/14/20/11/japanese-cherry-trees-324175_1280.jpg"
    ],
        "classEnrolled": [{}],
        "classCreated":[{}],
        "hosting":[{}],
        "bookmark":[{}],
        "reviews":[{ "name": "Gabby Caldwell",
        "rate": "3",
        "total_review": "13",
        "text": "Rachel was such a kind and knowledgeable guide. She took us to see some hidden coves that a lot of tourists probabaly miss. I’m keeping the map I made FOREVER!!!"}]
    } 
    ];
}

export default UserProfilesData;

I am trying to:

  • get the profile for a specific id.
  • get a specific information from a user of a specific id.

So for the first case, I would to search for the id: u0000000003 and return the whole json object and be able to extract later any information later. it could be address, or anything else.

For my second question, I would to do something like getProfilePicById(id) and return just the url which is in the field profilePic of the user with the id I want.

I try different solution but it's not working fine. Perhaps any librairy to do it easily ? I am trying to do it in a React Project - Javascript

Thanks Regards

Seb
  • 2,929
  • 4
  • 30
  • 73

1 Answers1

1

This method will return the pics array of given ID.

 function getProfilePicById(id){
     for(let k=0;k< Profiles.length;k++){
         if(Profiles[k].id==id){
           return Profiles[k].pics;
         }
     }
 }

This will return user profile object of given ID

function getProfile(id){
     for(let k=0;k< Profiles.length;k++){
         if(Profiles[k].id==id){
           return Profiles[k];
         }
     }
 }