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;
有没有办法使用lodash
摆脱多个if条件。
请帮忙
答案 0 :(得分:2)
这样的事可能有用:
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;