-1

I have this array

    this.complementaryFields = [
        {
            fieldName: 'complementaryLaser',
            label: 'Laser Hair Removal'
        },
        {
            fieldName: 'complementarySkin',
            label: 'Skin'
        },
        {
            fieldName: 'complementaryInjection',
            label: 'Cosmetic Injections'
        },
        {
            fieldName: 'complementaryNotSure',
            label: 'Not Sure'
        }
    ];

and I would like to create a new array out of it as below:

    this.complementaryFieldNames = [
        'complementaryLaser',
        'complementarySkin',
        'complementaryInjection',
        'complementaryNotSure'
    ];

How would you do it using lodash?

2

1 Answer 1

0

You can simply use lodash _.map to get that result:

const data = [{ fieldName: 'complementaryLaser', label: 'Laser Hair Removal' }, { fieldName: 'complementarySkin', label: 'Skin' }, { fieldName: 'complementaryInjection', label: 'Cosmetic Injections' }, { fieldName: 'complementaryNotSure', label: 'Not Sure' } ];

const result = _.map(data, 'fieldName')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

With ES6 you can do same with Array.map and destructuring:

const data = [{ fieldName: 'complementaryLaser', label: 'Laser Hair Removal' }, { fieldName: 'complementarySkin', label: 'Skin' }, { fieldName: 'complementaryInjection', label: 'Cosmetic Injections' }, { fieldName: 'complementaryNotSure', label: 'Not Sure' } ];

const result = data.map(({fieldName}) => fieldName)

console.log(result)

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

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.