简体   繁体   中英

How to implement multiple if statements in lodash

 import { get } from 'lodash';
import preferences from '../preferences.json';

function billingAggregation(data) {
    const response = {
        cardDetails: [],
        address: {},
    };
    if (data.items && data.items.paypal) {
        const creditCardObj = {
            firstName: get(data.items.paypal.address, 'firstName', undefined),
            lastName: get(data.items.paypal.address, 'lastName', undefined),
            cardType: preferences.cardTypes.PAYPAL,
            cardNumber: get(data.items.paypal, 'number', undefined),
            total: get(data.items.paypal, 'amount', undefined),
        };
        response.cardDetails.push(creditCardObj);
        response.address = get(data.items.paypal, 'address', {});
    }
    if (data && data.items && data.items.creditCards && data.items.creditCards.length > 0) {
        const creditCardObj = {
            firstName: get(data.items.creditCards[0], 'address.firstName', undefined),
            lastName: get(data.items.creditCards[0], 'address.lastName', undefined),
            cardType: get(data.items.creditCards[0], 'type', undefined),
            cardNumber: get(data.items.creditCards[0], 'number', undefined),
            total: get(data.items.creditCards[0], 'amount', undefined),
        };
        response.cardDetails.push(creditCardObj);
        response.address = get(data.items.creditCards[0], 'address', {});
    }
    if (data && data.items && data.items.unnamed
        && data.items.unnamed.giftCards
        && data.items.unnamed.giftCards.length > 0) {
        const giftCardObj = {
            firstName: get(data.items.unnamed.address, 'firstName', undefined),
            lastName: get(data.items.unnamed.address, 'lastName', undefined),
            cardType: preferences.cardTypes.GIFTCARD,
            cardNumber: get(data.items.unnamed.giftCards[0], 'number', undefined),
            total: get(data.items.unnamed.giftCards[0], 'amount', undefined),
        };
        response.cardDetails.push(giftCardObj);
        if (!response.address.id) {
            response.address = get(data.items.unnamed, 'address', {});
        }
    }
    return response;
}

export default billingAggregation;

Is there a way of getting rid of multiple if conditions using lodash .

Please help

Something like this might work:

import { get } from 'lodash';
import preferences from '../preferences.json';

function billingAggregation(data) {
    const response = {
        cardDetails: [],
        address: {},
    };

    response.address = get(data, 'items.paypal.address')
                    || get(data, 'items.creditCards[0].address')
                    || get(data, 'items.unnamed.address')
                    || {};

    if (get(data, 'items.paypal')) {
        response.cardDetails.push({
            firstName: get(data, 'items.paypal.address.firstName'),
            lastName: get(data, 'items.paypal.address.lastName'),
            cardType: preferences.cardTypes.PAYPAL,
            cardNumber: get(data, 'items.paypal.number'),
            total: get(data, 'items.paypal.amount'),
        });
    }

    if (get(data, 'items.creditCards[0]')) {
        response.cardDetails.push({
            firstName: get(data, 'items.creditCards[0].address.firstName'),
            lastName: get(data, 'items.creditCards[0].address.lastName'),
            cardType: get(data, 'items.creditCards[0].type'),
            cardNumber: get(data, 'items.creditCards[0].number'),
            total: get(data, 'items.creditCards[0].amount'),
        });
    }

    if (get(data, 'items.unnamed.giftCards[0]')) {
        response.cardDetails.push({
            firstName: get(data, 'items.unnamed.address.firstName'),
            lastName: get(data, 'items.unnamed.address.lastName'),
            cardType: preferences.cardTypes.GIFTCARD,
            cardNumber: get(data, 'items.unnamed.giftCards[0].number'),
            total: get(data, 'items.unnamed.giftCards[0].amount'),
        });
    }

    return response;
}

export default billingAggregation;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM