作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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条件。
请帮忙
最佳答案
像这样的东西可能会起作用:
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;
关于javascript - 如何在lodash中实现多个if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151002/
我是一名优秀的程序员,十分优秀!