簡體   English   中英

合並相同長度的 json

[英]merge the json with same length

我有兩組 json 數據我想合並在 json1 我有用username,subject,Geo 。在 json2 我有week,monthinwords 。我想合並username,subject,GEo,week,monthinwords

預計 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));

您可以使用減少傳播

 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)

您可以只 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