Home > Back-end >  How can I get sibling value in JS object?
How can I get sibling value in JS object?

Time:04-12

How can I get 'Pivot Grid', when I have 7? Is there method or can you help me with function?

WIDGET_TYPES = {

    PIVOT_GRID: {
        LABEL: 'Pivot Grid',
        ID: 7,
    },
    DATA_GRID: {
        LABEL: 'Grid',
        ID: 4,
    },
};

CodePudding user response:

you can use Object.values and find

Object.values - will convert your object to array wiht next structure:

[{LABEL: 'Pivot Grid',ID: 7,}, ...]

after conversion you can apply find to new generated array. In the find method you will pass your id(7) what need to find

then we need to take label from found element of array

const WIDGET_TYPES = {
    PIVOT_GRID: {
        LABEL: 'Pivot Grid',
        ID: 7,
    },
    DATA_GRID: {
        LABEL: 'Grid',
        ID: 4,
    },
};

const ID = 7;

const foundLabel = Object.values(WIDGET_TYPES).find(i => i.ID === ID)?.LABEL;

console.log('foundLabel: ', foundLabel)

  • Related