[英]How to join/merge two json objects using sailsjs-nodejs
Hello I am new to Sailsjs-Nodejs. 您好,我是Sailsjs-Nodejs的新手。 In my controller I have two JSON Objects I need to merge/join so I can make third to send response
在我的控制器中,我需要合并/加入两个JSON对象,因此我可以使第三个对象发送响应
res.send(obj1) output res.send(obj1)输出
[
{
total_fare: "376",
arrival_to: "ABV"
},
{
total_fare: "312",
arrival_to: "ACC"
},
{
total_fare: "432",
arrival_to: "BFN"
}
]
res.send(obj2) output res.send(obj2)输出
[
{
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 这是我的预期结果res.send(obj3)应该输出
[
{
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. 使用
lodash
程序包,我使用_.map()
遍历总票价,并使用_.find()
我通过arrival_to == city_code
查找第一次出现,并进行_.extend()
加入对象。
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. 我特别添加了
{total_fare: "111",arrival_to: "ACC"}
来显示给您和示例,说明存在2个或更多具有相同arrival_to
字段的对象。
// 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.