cmw
cmw

Reputation: 23

Assign a key value to another key value in JavaScript

I am currently learning JavaScript and I have come across the Object.assign() method but I am not quite sure how to apply it in project.

Here are my examples :

This is the reference array that each new profiles array should parse

const references = {
    "photos": {
        "paul": {
            "version": {
                "amsterdam": {
                    "image_url": "firstUrl",
                },
                "rotterdam": {
                    "image_url": "secondUrl",
                }
            }
        },
        "mary": {
            "version": {
                "berlin": {
                    "image_url": "thirdUrl",
                }
            }
        },
        "vincent": {
            "version": {
                "london": {
                    "image_url": "fourthUrl",
                },
                "paris": {
                    "image_url": "fifthUrl",
                },
                "prague": {
                    "image_url": "sixthUrl",
                }
            }
        }
    }
}

This is what my API request spits out

const profiles = {
    "paul": "rotterdam", 
    "vincent": "prague"
}

What I'd like to achieve is put the according image_url for each version of the photo like this

newArray = {
    "paul": "secondUrl", 
    "vincent": "sixthUrl"
}

But is it even possible ? Or is it not the purpose of Object.assign()?

Thank you very much!

Upvotes: 0

Views: 46

Answers (2)

Niko Chaffin-Chicas
Niko Chaffin-Chicas

Reputation: 23

First, the data structure you're working with is an Object, not an Array.

Second, Object.assign copies values from source objects to a target object, but does't allow for any manipulation of the structure in the process. It sounds like you want to use Object.fromEntries to obtain a new Object from your existing one.

const references = { /* full object abbreviated */ };

const profiles = {
    "paul": "rotterdam", 
    "vincent": "prague"
};

const result = Object.fromEntries(
    Object.entries(profiles).map(([user, version]) => {
        const photos = references.photos[user];
        const imageUrl = photos.version[version].image_url;
        return [user, imageUrl];
    })
);

Upvotes: 1

trincot
trincot

Reputation: 350079

I would create a new object using Object.entries and Object.fromEntries:

const references = {"photos": {"paul": {"version": {"amsterdam": {"image_url": "firstUrl",},"rotterdam": {"image_url": "secondUrl",}}},"mary": {"version": {"berlin": {"image_url": "thirdUrl",}}},"vincent": {"version": {"london": {"image_url": "fourthUrl",},"paris": {"image_url": "fifthUrl",},"prague": {"image_url": "sixthUrl",}}}}}

const profiles = {
    "paul": "rotterdam", 
    "vincent": "prague"
}

const result = Object.fromEntries(Object.entries(profiles).map(([k, v]) =>
    [k, references.photos[k]?.version?.[v]?.image_url]
));

console.log(result);

Alternatively you can use Object.assign instead of Object.fromEntries, in combination with the spread syntax:

const references = {"photos": {"paul": {"version": {"amsterdam": {"image_url": "firstUrl",},"rotterdam": {"image_url": "secondUrl",}}},"mary": {"version": {"berlin": {"image_url": "thirdUrl",}}},"vincent": {"version": {"london": {"image_url": "fourthUrl",},"paris": {"image_url": "fifthUrl",},"prague": {"image_url": "sixthUrl",}}}}}

const profiles = {
    "paul": "rotterdam", 
    "vincent": "prague"
}

const result = Object.assign({}, ...Object.entries(profiles).map(([k, v]) =>
    ({[k]: references.photos[k]?.version?.[v]?.image_url})
));

console.log(result);

Upvotes: 2

Related Questions