0

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!

1
  • Object.assingn() allow you to copy without reference an object in another, it doesn't fit your need here. Commented Jun 16, 2021 at 21:54

2 Answers 2

2

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);

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

1 Comment

This is just brilliant. Thank you a lot! These complex codes always look like voodoo spells to me before understanding them, I will keep on reading the docs and some day I'll be able to do it too!
1

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];
    })
);

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.