gpt4 book ai didi

javascript - JavaScript 中的对象没有返回值

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

如何在javascript中根据组合获取对象值?

我不知道如何根据函数调用的输入迭代对象并获取对象值。需要一些帮助。

预期输出应如下所示。

getValueCreditToBank(obj, "credit", "bank", "SGD");
getValueDebitToBank(obj, "debit", "bank", "THB");

下面的代码获取值但必须执行两个函数,是否有任何方法可以在单个函数调用中执行,

// getValueCreditToBank(obj, "credit", "bank", "SGD");
function getValueCreditToBank(provider, typein, typeout, source){
return provider.map(item => {
if (item.country_from[0].paymentIn[0].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout) {
return {
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[0].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[0].speed.number + "days",
...item
}
}
})
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}

// getValueDebitToBank(obj, "debit", "bank", "SGD");
function getValueDebitToBank(provider, typein, typeout, source){
return provider.map(item => {
if (item.country_from[0].paymentIn[1].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout) {
return {
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[1].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[1].speed.number + "days",
...item
}
}
})
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}

对象示例:

var obj = [{
"id": "identity1",
"fee": '2',
"rate": '0.5',
"targetAmount": '1000',
"country_from": [{
"currency": [
"SGD",
"USD"
],
"paymentIn": [{
"type": "credit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1.5"
}
},{
"type": "debit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1"
}
}],
"paymentout":[{
"type":"bank"
}]
}]
},
{
"id": "identity2",
"fee": '1',
"rate": '0.5',
"targetAmount": '1000',
"country_from": [{
"currency": [
"THB",
"USD"
],
"paymentIn": [{
"type": "debit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1"
}
}
],
"paymentout":[{
"type":"bank"
}]
}]
}]

预期输出:

//getValue(obj, "credit", "bank", "SGD"); should return object as
{id: "identity1",
fee: '2',
rate: '0.5',
currency: SGD,
paymentIn: "credit",
paymentOut: "bank",
paymentIn Fee: 1.5%,
targetAmount: 1000,
targetAmountwithPay: 506.485 //(((targetamount-fee)*rate)+credit fee))}
//getValue(obj, "debit", "bank", "THB"); should return object as
{id: "identity2",
fee: '1',
rate: '0.5',
currency: THB,
paymentIn: "debit",
paymentOut: "bank",
paymentIn Fee: 1%,
targetAmount: 1000,
targetAmountwithPay: 504.49 //(((targetamount-fee)*rate)+credit fee))}

最佳答案

我在您的代码中看到的第一个问题是您映射了所有数组,但只更改了一些元素。

也许您没有注意到这一点,因为您设置的过滤器正在传递所有数组元素,但如果没有,您会在最终数组中看到许多未定义的元素。

所以我建议您更新代码,引入过滤器:

function getValueCreditToBank(provider, typein, typeout, source){
return provider
.filter(item => (item.country_from[0].paymentIn[0].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout))
.map(item => ({
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[0].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[0].speed.number + "days",
...item
}))
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}

您在两个函数中执行几乎相同的任务,并且您没有正确使用参数,因此例如您可以通过 typein 参数确定 paymentIn 的索引:

function getValueToBank(provider, typein, typeout, source){
const paymentInIndex = typein === 'credit' ? 0 : 1;
return provider
.filter(item => (item.country_from[0].paymentIn[paymentInIndex].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout))
.map(item => ({
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[paymentInIndex].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[paymentInIndex].speed.number + "days",
...item
}))
}

然后你可以实现你的功能:

function getValueCreditToBank(provider, typeout, source){
return getValueToBank(provider, 'credit', typeout, source)
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}

function getValueDebitToBank(provider, typeout, source){
return getValueToBank(provider, 'debit', typeout, source)
}

因此,您从原始函数中删除了 typein 参数,因为它是由函数名称确定的。

这只是一个示例,您可以用不同的方式重写,更具可读性的是为您传递来过滤和映射数组的箭头函数命名。

关于javascript - JavaScript 中的对象没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468986/

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