需求:需要取出 a对象其中的mch_list (数组)的 goods_list(数组)的数据进行组合

需求:需要取出 a对象其中的mch_list (数组)的 goods_list(数组)的数据进行组合,结构图如下

{address_id: 57262, address: "{"id":57262,"name":"xxx","mobile":"xxxxxx","pr…district":"杨浦区","detail":"天安门路659","is_default":0}", mch_list: "[{"mch_id":0,"goods_list":[{"cart_id":1745,"sharer…,"show_length":0,"picker_coupon":{},"offline":0}]", agent: 1, use_integral: 1, …}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-blLEMlBQ-1617100219458)(C:\Users\i56\Desktop\image-20210330164656436.png)]

mch_list

"[{"mch_id":0,"goods_list":[{"cart_id":1745,"sharer_id":0,"use_attr":2,"num":3,"pack_attr":{"mch_id":0,"goods_id":"33","price":"450.00","num":"463","goods_price":"450.00","attr_agent_id":"20","pack_name":"箱(15件套)","pack_type":"3","pack_num":"1","freight":"9","weight":"1500.00","pic":"","no":"","pieces":null,"forehead":null,"is_member_price":false,"is_level":false,"level_price":"360.00","is_agent":"1"},"goods_id":33,"mch_id":0,"goods_name":"葛根木瓜餐","goods_pic":"https://testmall.sdforesee.com/addons/zjhj_mall/core/web/uploads/image/store_1/5eca84aa513e24b285117c105e059fbebdfcc4be.png","attr_list":[{"attr_group_name":"包装","attr_id":"20","attr_name":"箱(15件套)"}],"price":"1350.00","single_price":"450.00","weight":"1500.00","freight":"9","full_cut":"{\"pieces\":\"\",\"forehead\":\"\"}","integral":"{\"give\":\"0\",\"forehead\":\"\",\"more\":\"\"}","goods_cat_id":0,"id":33,"is_agent":"1","level_price":"1080.00","is_level":false,"give":0,"resIntegral":{"forehead":"0.00","forehead_integral":0},"goods_card_list":[],"is_agent_cat":"1","cat_id":[24],"is_fullcut":false,"express_price":11}],"name":"平台自营","form":{"is_form":0,"list":[]},"offline_name":null,"offline_mobile":null,"send_type":1,"shop_list":[],"is_shop":"","plugin_type":0,"agent":1,"total_price":"1350.00","level_price":"1080.00","integral":{"forehead":0,"forehead_integral":0},"coupon_list":[{"user_coupon_id":"99","sub_price":20,"min_price":100,"event":"1","begin_time":"2021.03.26 13:04","end_time":"2021.07.04 13:04","type":"1","appoint_type":"1","cat_id_list":null,"goods_id_list":"null","status":0,"min_price_desc":"满100.00元可用","event_desc":"分享红包"},{"user_coupon_id":"100","sub_price":20,"min_price":100,"event":"1","begin_time":"2021.03.26 15:53","end_time":"2021.07.04 15:53","type":"1","appoint_type":"1","cat_id_list":null,"goods_id_list":"null","status":0,"min_price_desc":"满100.00元可用","event_desc":"分享红包"}],"express_price":"11.00","express_allowance_price":0,"bulk_express_price":0,"total_team_discount":0,"offer_rule":{"is_allowed":0,"total_price":"0","msg":"自营商品,还差-1350元起送"},"is_area":0,"show":false,"show_length":0,"picker_coupon":{},"offline":0}]"

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uzLTNXzE-1617100187795)(C:\Users\i56\Desktop\image-20210330165031127.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LYQSUBDp-1617100187810)(C:\Users\i56\Desktop\image-20210330165700921.png)]

问题一:由于是对象,所以直接点,a.mch_list取到 mch_list,发现外形虽然是数组,但其真正类型是object,所以

打印a.mch_list[0] 发现是[,一顿操作猛如虎,第一步就gg

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PQEKWSIW-1617100187823)(C:\Users\i56\Desktop\image-20210330171401996.png)]

不能直接点,那就必须转换so

let ee = JSON.parse(a.mch_list)[0].goods_list
  console.log("ee", typeof (ee));

然后发现ee又是一个数组对象【】 Array.isArray(qq)

由于对对象比较陌生,我需要取出其中的数组放到 新的数组里面,于是使用了 Object.values(ee)

两个版本,这个是最初版本的,我以为是个对象,其实他就是个数组…,最后组合数据使用map,再用数组抱起来,自己多此一举,唉,老大催的

        let ee = JSON.parse(a.mch_list)[0].goods_list
        //取出对象的每个数组组成一个新数组
        let qq = Object.values(ee);
        let zz = []
        for (let index = 0; index < qq.length; index++) {
            //每个数组
            const element = qq[index];
            console.log("element", element);
            zz.push(({ pack_type: element.pack_attr.pack_type, goods_id: element.id, num: element.num }))
        }
        console.log("zz", zz);
        let ProductInfo = JSON.stringify(zz);
        this.check(ProductInfo, a, e)

第二个是优化之后的,发现其实就是数组后,内心是崩溃的,唉

        let ee = JSON.parse(a.mch_list)[0].goods_list
        let mlist=ee.map(item=>({
            pack_type: item.pack_attr.pack_type, goods_id: item.id, num: item.num
         }))     
        let ProductInfo = JSON.stringify(mlist);
        this.check(ProductInfo, a, e)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值