简体   繁体   English

合并相同长度的 json

[英]merge the json with same length

I have two set of json data I want to merge In json1 I have username,subject,Geo .In json2 I have week,monthinwords .I want to merge username,subject,GEo,week,monthinwords我有两组 json 数据我想合并在 json1 我有用username,subject,Geo 。在 json2 我有week,monthinwords 。我想合并username,subject,GEo,week,monthinwords

expected output预计 output

[
         {
            "UserName":"Sarathy Devaraju",
            "Subject":"Multi-layered Testing Discussion",
            "Geo":"Europe",
            "week":"Week 3",
            "monthinwords":"July"
            
         },
         {
            "UserName":"Sarathy Devaraju",
            "Subject":"Multi-layered Testing Discussion",
            "Geo":"Europe",
            "week":"Week 3",
            "monthinwords":"July"
            
         },
         {
            "UserName":"Sarathy Devaraju",
            "Subject":"Multi-layered Testing Discussion",
            "Geo":"Europe",
            "week":"Week 3",
            "monthinwords":"July"
            
         },
         {
            "UserName":"Sarathy Devaraju",
            "Subject":"Multi-layered Testing Discussion",
            "Geo":"Europe",
            "week":"Week 2",
            "monthinwords":"July"
            
         },
         {
            "UserName":"Sarathy Devaraju",
            "Subject":"Test Automation Discussion - Peleton International",
            "Geo":"Europe",
            "week":"Week 1",
            "monthinwords":"July"
         }
         
      ]

 var json1 = [ { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Test Automation Discussion - Peleton International", "Geo":"Europe" } ]; var json2 = [ { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 2", "monthinwords":"July" }, { "week":"Week 1", "monthinwords":"July" } ]; var obj3 = Object.assign(json1, json2); document.write(JSON.stringify(obj3));

You can use reduce with spread您可以使用减少传播

 var json1 = [ { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Multi-layered Testing Discussion", "Geo":"Europe" }, { "UserName":"Sarathy Devaraju", "Subject":"Test Automation Discussion - Peleton International", "Geo":"Europe" } ]; var json2 = [ { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 3", "monthinwords":"July" }, { "week":"Week 2", "monthinwords":"July" }, { "week":"Week 1", "monthinwords":"July" } ]; const result = json1.reduce((acc, rec, index) => { return [...acc, {...rec, ...json2[index] } ] }, []) console.log(result)

You can just map and merge the two:您可以只 map 并将两者合并:

 const json1=[{UserName:"Sarathy Devaraju",Subject:"Multi-layered Testing Discussion",Geo:"Europe"},{UserName:"Sarathy Devaraju",Subject:"Multi-layered Testing Discussion",Geo:"Europe"},{UserName:"Sarathy Devaraju",Subject:"Multi-layered Testing Discussion",Geo:"Europe"},{UserName:"Sarathy Devaraju",Subject:"Multi-layered Testing Discussion",Geo:"Europe"},{UserName:"Sarathy Devaraju",Subject:"Test Automation Discussion - Peleton International",Geo:"Europe"}]; const json2=[{week:"Week 3",monthinwords:"July"},{week:"Week 3",monthinwords:"July"},{week:"Week 3",monthinwords:"July"},{week:"Week 2",monthinwords:"July"},{week:"Week 1",monthinwords:"July"}]; const result = json1.map((el, i) => ({...el, ...json2[i]})); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM