抱着猫睡觉的鱼_
2021-09-24 17:43
采纳率: 96.8%
浏览 99
已结题

vue如何把一个数组里面的数据放到另一个数组的每一项里面

数据格式如下

let arr = [
    {
        "productName": "颜色",
        "productcode": "0001",
        "productId": "a80ff169-d6f6-4752-b980-fd818240410f"
    }
]

另一个数组

let arr2 = [
    {
        "productSkuName": "美年达1",
        "productSkuCode": "10",
        "productSkuId": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
        "remark": ""
    },
    {
        "productSkuName": "可乐1",
        "productSkuCode": "11",
        "productSkuId": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
        "remark": ""
    }
]

需要把arr中的数据添加到arr2中的每一项

  • 好问题 提建议
  • 收藏

2条回答 默认 最新

  • 程鹏; 2021-09-24 18:12
    已采纳
    
        let arr = [{
            "productName": "颜色",
            "productcode": "0001",
            "productId": "a80ff169-d6f6-4752-b980-fd818240410f"
        }]
    
        let arr2 = [{
                "productSkuName": "美年达1",
                "productSkuCode": "10",
                "productSkuId": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
                "remark": ""
            },
            {
                "productSkuName": "可乐1",
                "productSkuCode": "11",
                "productSkuId": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
                "remark": ""
            }
        ]
        
        arr2.forEach(item => {
            arr.map(item2 => {
                item = Object.assign(item,item2);
            })
        })
        
        console.log(arr2) 
        //[{productId: "a80ff169-d6f6-4752-b980-fd818240410f",
        // productName: "颜色",
        // productSkuCode: "10",
        // productSkuId: "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
        // productSkuName: "美年达1",
        // productcode: "0001",
        // remark: ""},
        // {
        //     productId: "a80ff169-d6f6-4752-b980-fd818240410f",
        //     productName: "颜色",
        //     productSkuCode: "11",
        //     productSkuId: "2ea2a723-1a6c-4545-92ce-2ef07547642f",
        //     productSkuName: "可乐1",
        //     productcode: "0001",
        //     remark: ""
        // }]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 落落Plus 2021-09-24 17:55

    arr必须是个数组吗
    let newArr = [];
    let mergeItem = {};
    arr.forEach(item => {
    mergeItem = {
    ...mergeItem,
    ...item
    }
    })
    arr2.forEach(item => {
    newArr.push({
    ...item,
    ...mergeItem
    })
    })

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题