最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to merge arrays in an array of JSON object? - Stack Overflow

programmeradmin1浏览0评论

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 0
Add a ment  | 

6 Answers 6

Reset to default 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);

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)

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;
}, []))];
发布评论

评论列表(0)

  1. 暂无评论