Merge two JSON data into one with particular key values

后端 未结 1 527
小蘑菇
小蘑菇 2021-01-28 10:25

I have two CSV files:

balldata.json

    [
      {
        \"id\": \"1\",
        \"color\": \"red\",

      },
            


        
相关标签:
1条回答
  • 2021-01-28 11:18

    You can do it like below

    const result = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, c, {color: q[i].color})));
    
    // Result
    
    [ 
     {court_id: 2001, ball_in_use: "1", color: "red"},
     {court_id: 2005, ball_in_use: "2", color: "blue"}
    ]
    
    

    else use below

    const result = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, {[c.court_id] : q[i].color})))
    
    //Result
    
    [
     {2001: "red"}, 
     {2005: "blue"}
    ]
    
    

    Working example https://codesandbox.io/s/react-example-b7bfm

    0 讨论(0)
提交回复
热议问题