【问题标题】:How to merge arrays in an array of JSON object?如何合并 JSON 对象数组中的数组?
【发布时间】:2019-06-15 00:28:14
【问题描述】:

我在express后端得到了一个JSON对象数组,每个对象都包含一个数组,键相同,称为projectTags

[ { projectTags: [ 'rer' ] },
  { projectTags: [ 'a10' ] },
  { projectTags: [ 'a10', 'c12', 'e14' ] },
  { projectTags: [ 'a10', 'e14', 'b11', 'c12', 'd13' ] } ]

我想将这些数组合并在一起,结果也不会有相同的标签。所以理想情况下它看起来像这样:

[ { projectTags: [ 'rer', 'a10', 'c12', 'e14', 'b11', 'd13' ] },]

那么我该怎么做才能做到这一点呢?

【问题讨论】:

    标签: javascript arrays json express


    【解决方案1】:

    您可以使用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;
    }, []))];
    

    【讨论】:

      【解决方案2】:

      这是我用 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);
                  }
      

      【讨论】:

        【解决方案3】:

        您可以将reduce您的地图合二为一,concating 尚未添加的元素。

        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)

        【讨论】:

          【解决方案4】:

          如果您不需要重复:

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

          【讨论】:

            【解决方案5】:

            您可以使用Set 并添加所有标签。

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

            【讨论】:

              【解决方案6】:

              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.map(o => o.projectTags)))]}];
                
              console.log(result);

              【讨论】:

                猜你喜欢
                • 2012-06-25
                • 1970-01-01
                • 2019-03-16
                • 2012-05-28
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多