gpt4 book ai didi

javascript - 如何在lodash中实现多个if语句

转载 作者:行者123 更新时间:2023-12-01 02:47:49 25 4
gpt4 key购买 nike

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com