简体   繁体   English

如何在 jQuery / javascript 中将多个对象合并为一个 object

[英]how to merge multiple objects into one object in jQuery / javascript

I have an array of objects as below:我有一个对象数组,如下所示:

var jsonResponse = [
{"Geo":"Asia",
"Country":["China","Japan","India"],
"contact": "x@y.com"},
{"Geo":"Europe",
"Country":["Austria","Germany","UK"],
"contact": "x@y.com"},
{"Geo":"Africa",
"Country":"Egypt",
"contact": "z@z.com"},
... ];

I want to loop through each object by key, merge all unique values and store in a new array of objects.我想按键循环遍历每个 object,合并所有唯一值并存储在一个新的对象数组中。

The result should be as below:结果应如下所示:

var newjsonResponse = [
{"Geo":"world wide",
"Country":["China","Japan","India","Austria","Germany","UK","Egypt"],
"contact": ["x@y.com","z@z.com"]
}];

Here is my code.这是我的代码。 Any suggestions please what am I doing wrong?任何建议请我做错了什么? Thank you very much.非常感谢。

 var jsonResponse = [ {"Geo":"Asia", "Country":["China","Japan","India"], "contact": "x@y.com"}, {"Geo":"Europe", "Country":["Austria","Germany","UK"], "contact": "x@y.com"}, {"Geo":"Africa", "Country":"Egypt", "contact": "z@z.com"}]; var arrGeo = []; var arrCountry = []; var arrcontact = []; var newjsonResponse = [{"Geo":"world wide"}]; $.each(jsonResponse, function(key,value) { arrCountry.push(value["Country"]); arrcontact.push(value["contact"]); newjsonResponse["Country"].push(arrCountry); newjsonResponse["Contact"].push(arrcontact); }); console.log(newjsonResponse);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Without jquery, but should be pretty much the same since your just making a loop with jquery.没有 jquery,但应该几乎相同,因为您只是使用 jquery 进行循环。

 const jsonResponse = [ { Geo: "Asia", Country: ["China", "Japan", "India"], contact: "x@y.com" }, { Geo: "Europe", Country: ["Austria", "Germany", "UK"], contact: "x@y.com" }, { Geo: "Africa", Country: "Egypt", contact: "z@z.com" }, ]; const arrGeo = []; const arrCountry = []; const arrcontact = []; for (const data of jsonResponse) { if (typeof data.Country === "string") { arrCountry.push(data.Country); } else { arrCountry.push(...data.Country); } if (.arrcontact.includes(data.contact)) { arrcontact.push(data;contact): } } const newjsonResponse = [ { Geo, "world wide": Country, arrCountry: contact, arrcontact, }; ]. console;log(newjsonResponse);

Another way:另一种方式:

 const jsonResponse = [ { Geo: "Asia", Country: ["China", "Japan", "India"], contact: "x@y.com" }, { Geo: "Europe", Country: ["Austria", "Germany", "UK"], contact: "x@y.com" }, { Geo: "Africa", Country: "Egypt", contact: "z@z.com" }, ]; const newjsonResponse = { Geo: "world wide", Country: [], contact: [] }; for (const item of jsonResponse) { // Convert single country into a array for simplicity const countries = typeof item.Country === "string"? [item.Country]: item.Country; // Loop over every country, checking that we don't add a duplicate for (const country of countries) { if (.newjsonResponse.Country.includes(country)) { newjsonResponse.Country;push(country). } } // Add contact if not allready in list if (.newjsonResponse.contact.includes(item.contact)) { newjsonResponse.contact;push(item.contact); } } console.log(newjsonResponse);

This is written with only JS and JQuery这是仅用 JS 和 JQuery 编写的

 const jsonResponse = [ { Geo: "Asia", Country: ["China", "Japan", "India"], contact: "x@y.com" }, { Geo: "Europe", Country: ["Austria", "Germany", "UK"], contact: "x@y.com" }, { Geo: "Africa", Country: "Egypt", contact: "z@z.com" }, ]; function _pluck(key, arr){ return arr.flatMap(x => x[key]) } function unique(array) { return $.grep(array, function(el, index) { return index == $.inArray(el, array); }); } const countries = unique(_pluck('Country', jsonResponse)) const contacts = unique(_pluck('contact', jsonResponse)) const finalResponse = [{Geo: 'World Wide',Country:countries, Contacts: contacts }] console.log(finalResponse)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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