合并数组中具有相同属性值的对象

时间:2019-01-17 16:54:08

标签: javascript arrays json object array-merge

我有一个对象数组。我想合并具有相同“标签”属性的数组中的对象。使用我的代码(粘贴在下面),我可以合并,但是合并发生在所有对象数组而不是数组中的对象上。

var pages= [
    {
         name: "page1", 
         list:{
           fields:[
             {name: "sHist", label: "Range", type: "long", searchable: false},
             {name: "sFuture", label: "Range", type: "long", searchable: false},
             {name: "ordersinPage1", label: "Orders", type: "long", searchable: false}
           ]
          }
    },
    {
         name: "page2", 
         list:{
           fields:[
             {name: "needs", label: "Needs", type: "long", searchable: false},
             {name: "fulfil", label: "Fulfill", type: "long", searchable: false},
             {name: "ordersinPage2", label: "Orders", type: "long", searchable: false}
           ]
          }
    }

  ]

我尝试的代码如下:

 let seen={};var totalFieldsObject ;
 pages.map((graphPage) => {
  totalFieldsObject = graphPage.list.fields.filter((entry) => {
      if (seen.hasOwnProperty(entry.label)) {
        let previous;
        previous = seen[entry.label];
        previous.name.push(entry.name);
        return false;
      }
      if (!Array.isArray(entry.name)) {
        entry.name = [entry.name];
      }
      seen[entry.label] = entry;
      return true;
    });
    });
  console.log(pages)

预期输出:

var exprectedOutput=[
    {
         name: "page1", 
         list:{
           fields:[
             {name: ["sHist","sFuture"], label: "Range", type: "long", searchable: false}, 
             {name: ["ordersinPage1"], label: "Orders", type: "long", searchable: false}
           ]
          }
    },
    {
         name: "page2", 
         list:{
           fields:[
             {name: ["needs"], label: "Needs", type: "long", searchable: false},
             {name: ["fulfil"], label: "Fulfill", type: "long", searchable: false},
             {name: ["ordersinPage2"], label: "Orders", type: "long", searchable: false}
           ]
          }
    },

  ]

1 个答案:

答案 0 :(得分:2)

您将需要Array.reduce()来将具有相同标签的对象分组:

var pages = [{
    name: "page1",
    list: {
      fields: [{
          name: "sHist",
          label: "Range",
          type: "long",
          searchable: false
        },
        {
          name: "sFuture",
          label: "Range",
          type: "long",
          searchable: false
        },
        {
          name: "ordersinPage1",
          label: "Orders",
          type: "long",
          searchable: false
        }
      ]
    }
  },
  {
    name: "page2",
    list: {
      fields: [{
          name: "needs",
          label: "Needs",
          type: "long",
          searchable: false
        },
        {
          name: "fulfil",
          label: "Fulfill",
          type: "long",
          searchable: false
        },
        {
          name: "ordersinPage2",
          label: "Orders",
          type: "long",
          searchable: false
        }
      ]
    }
  }

]

const result = pages.map(page =>
  page.list.fields.reduce((all, curr) => {
    const ndx = all.findIndex(e => e.label === curr.label); // look for the current element in the list
    if (ndx > -1) { 
      // if found, concat the names in an array
      // array.flat() will transform this : [["a"], "b"] into : ["a", "b"]
      all[ndx].name = [all[ndx].name, curr.name].flat();
    } else {
      // otherwise, edit the name property to become an array and push it.      
      all.push({ ...curr, name: [curr.name]})
    }
    return all;
  }, [])
)

console.log(result)