给出以下对象数组:
[{
"JobPosition": [
{
"node": {
"jobTitle": "Front end",
"jobLocation": "LOCATION: London",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS__us"
}
}
},
{
"node": {
"jobTitle": "Back end developer",
"jobLocation": "LOCATION: Milan ",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___it"
}
}
},
{
"node": {
"jobTitle": "Android Developer",
"jobLocation": "LOCATION: London",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___de"
}
}
}
]},
{
"JobsPage": [
{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS__us",
"pageName": "Job",
"seoTitle": "Jobs",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
},
{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___it",
"pageName": "Job",
"seoTitle": "Work with us",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
},
{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___de",
"pageName": "Job",
"seoTitle": "Jobs",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
}]
}]
我需要加入具有相同ID的条目并添加该语言的密钥,以实现此数据结构:
[{
"us": [{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS__us",
"pageName": "Job",
"seoTitle": "Jobs",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
},
{
"node": {
"jobTitle": "iOS Mobile Developer",
"jobLocation": "LOCATION: MILANO (IT)",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS__us"
}
}
}
]
}, {
"it": [{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___it",
"pageName": "Job",
"seoTitle": "Lavora con noi",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
},
{
"node": {
"jobTitle": "Front End",
"jobLocation": "LOCATION: MILANO (IT)",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___it"
}
}
}
]
}, {
"de": [{
"node": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___de",
"pageName": "Job",
"seoTitle": "Lavora con noi",
"seoDescription": "Lorem Ipsum",
"seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum",
}
},
{
"node": {
"jobTitle": "Back end developer",
"jobLocation": "LOCATION: Milan",
"jobTest": {
"id": "c3FDAuPSTfqIkIUi6qcOmqS___de"
}
}
}
]
}]
我只设法加入一个对象中的所有内容,任何想法如何在javascript中完成此操作?也许filter和reduce方法可能会有所帮助,但我对它很新。感谢
答案 0 :(得分:0)
Array.reduce()
+ Array.map()
解决方案:
var data = [{ "JobPosition": [ { "node": { "jobTitle": "iOS Mobile Developer", "jobLocation": "LOCATION: MILANO (IT)", "jobTest": { "id": "c3FDAuPSTfqIkIUi6qcOmqS__us" } } }, { "node": { "jobTitle": "Android Mobile Developer - IT", "jobLocation": "LOCATION: MILANO (IT)", "jobTest": { "id": "c3FDAuPSTfqIkIUi6qcOmqS___it" } } }, { "node": { "jobTitle": "Android Mobile Developer - DE", "jobLocation": "LOCATION: MILANO (IT)", "jobTest": { "id": "c3FDAuPSTfqIkIUi6qcOmqS___de" } } } ]},
{ "JobsPage": [ { "node": { "id": "c3FDAuPSTfqIkIUi6qcOmqS__us", "pageName": "Job", "seoTitle": "Jobs", "seoDescription": "Lorem Ipsum", "seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum", } }, { "node": { "id": "c3FDAuPSTfqIkIUi6qcOmqS___it", "pageName": "Job", "seoTitle": "Lavora con noi", "seoDescription": "Lorem Ipsum", "seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum", } }, { "node": { "id": "c3FDAuPSTfqIkIUi6qcOmqS___de", "pageName": "Job", "seoTitle": "Jobs", "seoDescription": "Lorem Ipsum", "seoKeywords": "Lorem Ipsum, Lorem Ipsum, Lorem Ipsum", } }] }
],
groups = data[0].JobPosition.concat(data[1].JobsPage).reduce(function(r, o){
var cc = (o.node.id || o.node.jobTest.id).substr(-2); // extracting country code
(r[cc])? r[cc].push(o) : r[cc] = [o];
return r;
}, {}),
result = Object.keys(groups).map(function(k){
var o = {};
o[k] = groups[k];
return o;
});
console.log(result);
答案 1 :(得分:0)
使用map和filter,我添加了一个JSON.stringify来帮助您更好地查看结果:
import 'rxjs/add/observable/throw';