I got an array of JSON object in express backend like this, which each object contains an array, the key is the same called projectTags
:
[ { projectTags: [ 'rer' ] },
{ projectTags: [ 'a10' ] },
{ projectTags: [ 'a10', 'c12', 'e14' ] },
{ projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] } ]
And I want to merge these arrays together, which the result won't have the same tag too. So ideally it will look like this:
[ { projectTags: [ 'rer', 'a10', 'c12', 'e14', 'b11', 'd13' ] },]
So how should I do to achieve this?
I got an array of JSON object in express backend like this, which each object contains an array, the key is the same called projectTags
:
[ { projectTags: [ 'rer' ] },
{ projectTags: [ 'a10' ] },
{ projectTags: [ 'a10', 'c12', 'e14' ] },
{ projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] } ]
And I want to merge these arrays together, which the result won't have the same tag too. So ideally it will look like this:
[ { projectTags: [ 'rer', 'a10', 'c12', 'e14', 'b11', 'd13' ] },]
So how should I do to achieve this?
Share Improve this question asked Jan 21, 2019 at 15:21 RiverRiver 10112 bronze badges 06 Answers
Reset to default 4With concat
you can make one array, and with Set you can get unique values:
const data = [ { projectTags: [ 'rer' ] },{ projectTags: [ 'a10' ] }, { projectTags: [ 'a10', 'c12', 'e14' ] }, { projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] } ];
const result = [{ projectTags: [...new Set([].concat(...data.map(o => o.projectTags)))]}];
console.log(result);
You can reduce
your maps into one, concat
ing elements which was not added yet.
const arr = [{projectTags:["rer"]},{projectTags:["a10"]},{projectTags:["a10","c12","e14"]},{projectTags:["a10","e14","b11","c12","d13"]}];
const resp = [{ projectTags: arr.reduce((a, e) => {
const notIncludedYet = e.projectTags.filter(x => !a.includes(x));
return a.concat(notIncludedYet)
}, []) }]
console.log(resp)
You could take a Set
and add all tags.
var data = [{ projectTags: ['rer'] }, { projectTags: ['a10'] }, { projectTags: ['a10', 'c12', 'e14'] }, { projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }],
result = [{ projectTags: Array.from(
data.reduce(
(s, { projectTags }) => (
projectTags.forEach(Set.prototype.add, s),
s
),
new Set
)
) }];
console.log(result) ;
If you need no duplicates:
var arr = [ { projectTags: ['rer'] },
{ projectTags: ['a10'] },
{ projectTags: ['a10', 'c12', 'e14'] },
{ projectTags: ['a10', 'e14', 'b11', 'c12', 'd13'] }];
var arr2 = [];
arr.forEach(function (e) {
var i = e.projectTags;
for (var x = 0; x < i.length; x++) {
if (arr2.indexOf(i[x]) == -1) arr2.push(i[x]);
}
});
console.log(arr2);
Here is the code that I have written in the JavaScript. Hope this help you.
//The list of the data provided in the question.
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);
}
You can use reduce to create an array with all the elements together, and then with Set remove all duplicates:
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;
}, []))];