Javascript 如何在JSON对象的数组中合并数组?
我在express后端得到了一个JSON对象数组,如下所示,每个对象都包含一个数组,键与projectTags相同: 我想把这些数组合并在一起,结果不会有相同的标签。因此理想情况下,它将如下所示:Javascript 如何在JSON对象的数组中合并数组?,javascript,arrays,json,express,Javascript,Arrays,Json,Express,我在express后端得到了一个JSON对象数组,如下所示,每个对象都包含一个数组,键与projectTags相同: 我想把这些数组合并在一起,结果不会有相同的标签。因此理想情况下,它将如下所示: [ { projectTags: [ 'rer', 'a10', 'c12', 'e14', 'b11', 'd13' ] },] 那么我该怎么做才能做到这一点呢?您可以将地图简化为一个尚未添加的具体元素 const arr=[{projectTags:[rer]},{projectTags:[a
[ { projectTags: [ 'rer', 'a10', 'c12', 'e14', 'b11', 'd13' ] },]
那么我该怎么做才能做到这一点呢?您可以将地图简化为一个尚未添加的具体元素
const arr=[{projectTags:[rer]},{projectTags:[a10]},{projectTags:[a10,c12,e14]},{projectTags:[a10,e14,b11,c12,d13]};
const resp=[{projectTags:arr.reducea,e=>{
const notIncludedYet=e.projectTags.filterx=>!a.includesx;
返回a.concatnotincluded尚未包含
}, [] }]
logresp您可以将地图缩减为一个包含尚未添加元素的元素
const arr=[{projectTags:[rer]},{projectTags:[a10]},{projectTags:[a10,c12,e14]},{projectTags:[a10,e14,b11,c12,d13]};
const resp=[{projectTags:arr.reducea,e=>{
const notIncludedYet=e.projectTags.filterx=>!a.includesx;
返回a.concatnotincluded尚未包含
}, [] }]
console.logresp使用concat可以创建一个数组,使用Set可以获得唯一值:
const data=[{projectTags:['rer']},{projectTags:['a10']},{projectTags:['a10',c12',e14']},{projectTags:['a10',e14',b11',c12',d13']};
const result=[{projectTags:[…new Set[].concat…data.mapo=>o.projectTags]}];
console.logresult 使用concat可以创建一个数组,使用Set可以获得唯一值:
const data=[{projectTags:['rer']},{projectTags:['a10']},{projectTags:['a10',c12',e14']},{projectTags:['a10',e14',b11',c12',d13']};
const result=[{projectTags:[…new Set[].concat…data.mapo=>o.projectTags]}];
console.logresult 您可以选择一组并添加所有标记
var data=[{projectTags:['rer']},{projectTags:['a10']},{projectTags:['a10',c12',e14']},{projectTags:['a10',e14',b11',c12',d13']},
结果=[{projectTags:Array.from
数据还原
s、 {projectTags}=>
projectTags.forEachSet.prototype.add,s,
s
,
新集
}];
console.logresult;
您可以选择一组并添加所有标记
var data=[{projectTags:['rer']},{projectTags:['a10']},{projectTags:['a10',c12',e14']},{projectTags:['a10',e14',b11',c12',d13']},
结果=[{projectTags:Array.from
数据还原
s、 {projectTags}=>
projectTags.forEachSet.prototype.add,s,
s
,
新集
}];
console.logresult;
如果不需要副本:
const arr = [ { projectTags: ['rer'] },
{ projectTags: ['a10'] },
{ projectTags: ['a10', 'c12', 'e14'] },
{ projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }];
const withoutDuplicates = [... new Set(arr.reduce((newArray, element) => {
newArray.push(...element.projectTags);
return newArray;
}, []))];
var arr=[{projectTags:['rer']},
{projectTags:['a10']},
{projectTags:['a10','c12','e14']},
{projectTags:['a10','e14','b11','c12','d13']};
var arr2=[];
arr.foreach函数e{
var i=项目标签;
对于var x=0;xconst arr = [ { projectTags: ['rer'] },
{ projectTags: ['a10'] },
{ projectTags: ['a10', 'c12', 'e14'] },
{ projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }];
const withoutDuplicates = [... new Set(arr.reduce((newArray, element) => {
newArray.push(...element.projectTags);
return newArray;
}, []))];
var arr=[{projectTags:['rer']},
{projectTags:['a10']},
{projectTags:['a10','c12','e14']},
{projectTags:['a10','e14','b11','c12','d13']};
var arr2=[];
arr.foreach函数e{
var i=项目标签;
对于var x=0;x var dataList = [
{ projectTags: [ 'rer' ] },
{ projectTags: [ 'a10' ] },
{ projectTags: [ 'a10', 'c12', 'e14' ] },
{ projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] }
];
//Final Result
var dataListAfterMerge = [];
//the list of unique items to be added.
var items = [];
//to verify if the item is already present in the new list (i.e. items).
var isAdded = false;
//Call this function on button click or any place you needed.
merge = function() {
for (var data of dataList){
for(var item of data.projectTags) {
for(var newItem of items) {
if(item == newItem) {
isAdded = true;
break;
}
}
if(!isAdded) {
items.push(item);
}
isAdded = false;
}
}
dataListAfterMerge.push({projectTags: items});
console.log(dataListAfterMerge);
}
下面是我用JavaScript编写的代码。 希望这对你有帮助 //问题中提供的数据列表
var dataList = [
{ projectTags: [ 'rer' ] },
{ projectTags: [ 'a10' ] },
{ projectTags: [ 'a10', 'c12', 'e14' ] },
{ projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] }
];
//Final Result
var dataListAfterMerge = [];
//the list of unique items to be added.
var items = [];
//to verify if the item is already present in the new list (i.e. items).
var isAdded = false;
//Call this function on button click or any place you needed.
merge = function() {
for (var data of dataList){
for(var item of data.projectTags) {
for(var newItem of items) {
if(item == newItem) {
isAdded = true;
break;
}
}
if(!isAdded) {
items.push(item);
}
isAdded = false;
}
}
dataListAfterMerge.push({projectTags: items});
console.log(dataListAfterMerge);
}
您可以使用reduce创建一个包含所有元素的数组,然后使用Set删除所有重复项:
const arr = [ { projectTags: ['rer'] },
{ projectTags: ['a10'] },
{ projectTags: ['a10', 'c12', 'e14'] },
{ projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }];
const withoutDuplicates = [... new Set(arr.reduce((newArray, element) => {
newArray.push(...element.projectTags);
return newArray;
}, []))];
您可以使用reduce创建一个包含所有元素的数组,然后使用Set删除所有重复项:
const arr = [ { projectTags: ['rer'] },
{ projectTags: ['a10'] },
{ projectTags: ['a10', 'c12', 'e14'] },
{ projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }];
const withoutDuplicates = [... new Set(arr.reduce((newArray, element) => {
newArray.push(...element.projectTags);
return newArray;
}, []))];