json Map功能:数组内的嵌套对象未连续打印

kse8i1jr  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(226)

给定下面的一个函数,我想通过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没有打印出来?

gdrx4gfi

gdrx4gfi1#

导致您的特定错误的问题是JSON作为对象返回,而对象没有.map方法。一个选择是像这样使用Object.values方法:

const manipulateRequest = Object.values(res).map((obj, index) => {
     return {
                    _s: res.data.shipping_rates[index].reguler.data[index]._s
    }
}

但是,我不确定您使用此代码返回具有_s属性的对象的目的是什么。您可能会尝试简单地返回_s属性的值,并将其存储为manipulateRequest数组中的值:

const manipulateRequest = Object.values(res).map((obj, index) => {
     return obj.shipping_rates[index].reguler.data[index]._s;
}

明智的做法是对此进行一些验证,以确保属性存在并且属性数据类型正确。

相关问题