给定下面的一个函数,我想通过manipulateValidateReqRes函数的返回值将_s从resMap到另一个函数
CODE WAS UPDATED BELOW
为什么我不能从map www.example.com函数返回_sres.map?它说TypeError:对象没有成员“map”
已更新
为了清楚地解释和更新调试过程,我在下面给予了我的最新代码
const res = {
"valid_data": [
{
"shipper_name": "automate sender1",
"rts_reasons": [
"a reason"
],
"rts_score": 0
},
{
"shipper_name": "automate sender2",
"rts_reasons": [
"a reason"
],
"rts_score": 0
}
],
"shipping_rates": [
{
"reguler": {
"summary_price": "7.000",
"data": [
{
"_s": "9917xxx",
}
]
},
"reguler": {
"summary_price": "7.000",
"data": [
{
"_s": "9918xxx",
}
]
}
}
],
"errors": [
[],
[]
]
}
const manipulateRequest = Object.values(res).map((obj) => {
return {
// what key do you want?
shipper_name: res.valid_data[0].shipper_name
//_s: i want return _s value to manipulateRequest from res variable
}
})
const postBulkPayload = {
"filename": "filename1.xlsx",
"total_order": manipulateRequest.length,
"is_use_new_payment": true,
"template_name": "bulk-order-with-postal_code-and-sub_district_name",
"details": manipulateRequest,
"cancelToken": {
"promise": {}
}
}
console.log(postBulkPayload)
因为结果是
{
filename: 'filename1.xlsx',
total_order: 3,
is_use_new_payment: true,
template_name: 'bulk-order-with-postal_code-and-sub_district_name',
details: [
{ shipper_name: 'automate sender1' },
{ shipper_name: 'automate sender1' },
{ shipper_name: 'automate sender1' }
],
cancelToken: { promise: {} }
}
为什么automate sender2没有打印出来?
1条答案
按热度按时间gdrx4gfi1#
导致您的特定错误的问题是JSON作为对象返回,而对象没有
.map
方法。一个选择是像这样使用Object.values
方法:但是,我不确定您使用此代码返回具有_s属性的对象的目的是什么。您可能会尝试简单地返回_s属性的值,并将其存储为
manipulateRequest
数组中的值:明智的做法是对此进行一些验证,以确保属性存在并且属性数据类型正确。