Ad

Duplicate Key In Javascript Object

- 1 answer

I've been working on voxeet-react-components and found something funny. Some objects in the source code has duplicated keys. For example:

localizedStrings.js

import LocalizedStrings from 'react-localization';
export const strings = new LocalizedStrings({
    en: {
       // ...rest omitted 
       presenter: "Attendees",    // line 75
       joined: "Joined",
       invited: "Waiting on",
       presenter: "Presenter",    // line 79
       // ... rest omitted
    },
    // ... rest omitted
});

How does react-localization work for duplicated keys? And that is not all.

VoxeetReactComponent.js

// ...rest omitted
module.exports = {
    reducer,
    StatusButton,
    ReplayButton,
    StatusCard,
    ConferenceRoom,
    Buttons:{
        ToggleMicrophoneButton,
        ToggleModeButton,        // line 32
        ToggleRecordingButton,
        ToggleScreenShareButton,
        ToggleSettingsButton,
        ToggleVideoButton,
        HangupButton,
        ToggleFullScreenButton,
        ToggleModeButton,        // line 39
        HangUpButtonBottomBar,
        Toggle3DAudioButton,
        ToggleExternalLiveButton,
        ToggleAttendeesChatButton,
        TogglePSTN,
        ToggleAttendeesListButton
    }
}

Is it just some kind of mistake? Otherwise, how does react work for duplicated components?

Ad

Answer

The object doesn't have duplicate keys, the object literal in the source code does.

This is an error (or at least nonsense) on the part of the author. Only the last value will be assigned for the given property.

const foo = {
    a: 1,
    b: 2,
    b: 3,
    c: 4
};

console.log(foo);

Ad
source: stackoverflow.com
Ad