简体   繁体   中英

How to join/merge two json objects using sailsjs-nodejs

Hello I am new to Sailsjs-Nodejs. In my controller I have two JSON Objects I need to merge/join so I can make third to send response

res.send(obj1) output

[
  {
    total_fare: "376",
    arrival_to: "ABV"
  },
  {
    total_fare: "312",
    arrival_to: "ACC"
  },
  {
    total_fare: "432",
    arrival_to: "BFN"
  }
]

res.send(obj2) output

[
  {
    url: "nigeria.php",
    country: "Nigeria",
    city_code: "ABV"
  },
  {
    url: "ghana.php",
    country: "Ghana",
    city_code: "ACC"
  },
  {
    url: "south-africa.php",
    country: "South Africa",
    city_code: "BFN"
  }
]

Here is my expected result res.send(obj3) should output

[
  {
    url: "nigeria.php",
    country: "Nigeria",
    city_code: "ABV",
    total_fare: "376",
    arrival_to: "ABV"
  },
  {
    url: "ghana.php",
    country: "Ghana",
    city_code: "ACC",
    total_fare: "312",
    arrival_to: "ACC"
  },
  {
    url: "south-africa.php",
    country: "South Africa",
    city_code: "BFN",
    total_fare: "432",
    arrival_to: "BFN"
  }
]

You can probably do as follows without modifying the source arrays;

 var arr = [{total_fare: "376",arrival_to: "ABV"}, {total_fare: "312",arrival_to: "ACC"}, {total_fare: "432",arrival_to: "BFN"}], brr = [{url: "nigeria.php",country: "Nigeria",city_code: "ABV"}, {url: "ghana.php",country: "Ghana",city_code: "ACC"}, {url: "south-africa.php",country: "South Africa",city_code: "BFN"}], merged = arr.map((o,i) => Object.assign({},o,brr[i])); console.log(merged); 

Check this example.

Using lodash package I'm using _.map() to walk through total fares, and using _.find() I'm finding first ocurrence by arrival_to == city_code and do _.extend() joining objects.


This example is helpful when Your array of objects are not sequential and also if there will be more total fares than elements in country list .

I've specially added {total_fare: "111",arrival_to: "ACC"} to show You and example when there is 2 or more objects with same arrival_to field.

 // const _ = require('lodash'); //uncomment on serverside usage let totalFares = [{total_fare: "376",arrival_to: "ABV"}, {total_fare: "312",arrival_to: "ACC"}, {total_fare: "432",arrival_to: "BFN"}, {total_fare: "111",arrival_to: "ACC"}]; let countries = [{url: "nigeria.php",country: "Nigeria",city_code: "ABV"}, {url: "ghana.php",country: "Ghana",city_code: "ACC"}, {url: "south-africa.php",country: "South Africa",city_code: "BFN"}]; totalFares = _.map(totalFares, (totalFare) => { _.extend(totalFare, _.find(countries, (country) => country.city_code == totalFare.arrival_to)); return totalFare; }); console.log(totalFares); 
 <script src="https://raw.githubusercontent.com/lodash/lodash/4.14.1/dist/lodash.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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