数组index相同的数组进行合并

文章展示了如何使用JavaScript将两个数组中的对象按照索引(index)进行合并。通过map或forEach方法,将formJson数组中的特定属性(如title和uuid)合并到lotteryQuestion数组对应的对象上。
摘要由CSDN通过智能技术生成

index相等的数据进行合并 

                                let formJson = [{
                                        remind: "请输入电话",
                                        sort: 0,
                                        title: "电话",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b9"
                                    },
                                    {
                                        remind: "请输入姓名",
                                        sort: 0,
                                        title: "姓名",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b7"
                                    },
                                    {
                                        remind: "请输入职位",
                                        sort: 0,
                                        title: "职位",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b8"
                                    },
                                ];
                                let lotteryQuestion = [{
                                        id: "18817164145",
                                        typeId: "1",
                                        value: "789"
                                    },
                                    {
                                        id: "19017164145",
                                        typeId: "2",
                                        value: "456"
                                    },
                                    {
                                        id: "1717164145",
                                        typeId: "3",
                                        value: "123"
                                    },
                                ];
                                // index相等的数据进行合并
                              let newArr=  lotteryQuestion.map((item,index)=>{
                                  return {...formJson[index],...item}
                                })
                                console.log(newArr,'测试');

效果图:

index相同的部分数据进行合并

                                let formJson = [{
                                        remind: "请输入电话",
                                        sort: 0,
                                        title: "电话",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b9"
                                    },
                                    {
                                        remind: "请输入姓名",
                                        sort: 0,
                                        title: "姓名",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b7"
                                    },
                                    {
                                        remind: "请输入职位",
                                        sort: 0,
                                        title: "职位",
                                        type: 1,
                                        uuid: "15e343c1-060d-TL51f-8ace-23a968e9e2b8"
                                    },
                                ];
                                let lotteryQuestion = [{
                                        id: "18817164145",
                                        typeId: "1",
                                        value: "789"
                                    },
                                    {
                                        id: "19017164145",
                                        typeId: "2",
                                        value: "456"
                                    },
                                    {
                                        id: "1717164145",
                                        typeId: "3",
                                        value: "123"
                                    },
                                ];
                                // index相等的部分数据进行合并
                                formJson.map((item,index)=>{
                                   lotteryQuestion[index].title=item.title;
                                   lotteryQuestion[index].uuid = item.uuid
                                })
                                console.log(lotteryQuestion,'部分数据合并');

效果图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值