2

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?

0

6 Answers 6

4

With 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);

Sign up to request clarification or add additional context in comments.

Comments

2

You can reduce your maps into one, concating 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)

Comments

1

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)  ;
    

Comments

0

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);

Comments

0

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);
            }

Comments

0

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;
}, []))];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.