在JavaScript中合并JSON格式的对象数组

wnavrhmk  于 2023-06-07  发布在  Java
关注(0)|答案(2)|浏览(154)

我有一个这样的json文件:

[
  {
    "stores": [ 
        {
          "name" : "My store",
          "location" : "NY"
         },
         { 
          "name" : "Other store",
          "location" : "FL"
         },
         {
          "name" : "My other store",
          "location" : "NY"
         }
     ],
  },
  {
    "stores": [ 
        {
          "name" : "Another My store",
          "location" : "NY"
         },
         { 
          "name" : "Some Other store",
          "location" : "FL"
         },
         {
          "name" : "Secondary store",
          "location" : "NY"
         }
     ],
  }
]

我想把数据合并成:

[
        {
          "name" : "My store",
          "location" : "NY"
         },
         { 
          "name" : "Other store",
          "location" : "FL"
         },
         {
          "name" : "My other store",
          "location" : "NY"
         },
         {
          "name" : "Another My store",
          "location" : "NY"
         },
         { 
          "name" : "Some Other store",
          "location" : "FL"
         },
         {
          "name" : "Secondary store",
          "location" : "NY"
         }
]

我从一个循环开始,但我不知道如何将它们全部分组为1:

const input = [
  {
    "stores": [ 
        {
          "name" : "My store",
          "location" : "NY"
         },
         { 
          "name" : "Other store",
          "location" : "FL"
         },
         {
          "name" : "My other store",
          "location" : "NY"
         }
     ],
  },
  {
    "stores": [ 
        {
          "name" : "Another My store",
          "location" : "NY"
         },
         { 
          "name" : "Some Other store",
          "location" : "FL"
         },
         {
          "name" : "Secondary store",
          "location" : "NY"
         }
     ],
  }
];

let grouped = []
input.forEach(item => {
    const store = Object.keys(item.stores)
  console.log(store)
})
anhgbhbe

anhgbhbe1#

使用Array#flatMap()

function mergeStores(array) {
  // Convert each element to its 'stores' array, then flat all of them
  return array.flatMap(element => element.stores);
}

试试看:

console.config({ maximize: true });

function mergeStores(array) {
  return array.flatMap(element => element.stores);
}

const input = [
    {
        stores: [
            { name: 'My store', location: 'NY' },
            { name: 'Other store', location: 'FL' },
            { name: 'My other store', location: 'NY' }
        ]
    },
    {
        stores: [
            { name: 'Another My store', location: 'NY' },
            { name: 'Some Other store', location: 'FL' },
            { name: 'Secondary store', location: 'NY' }
        ]
    }
];

console.log(mergeStores(input))
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
rqmkfv5c

rqmkfv5c2#

const input = [
  {
    stores: [
      {
        name: "My store",
        location: "NY",
      },
      {
        name: "Other store",
        location: "FL",
      },
      {
        name: "My other store",
        location: "NY",
      },
    ],
  },
  {
    stores: [
      {
        name: "Another My store",
        location: "NY",
      },
      {
        name: "Some Other store",
        location: "FL",
      },
      {
        name: "Secondary store",
        location: "NY",
      },
    ],
  },
];

// Create the result object the way you wanted
const result = [{stores: []}]
// Loop through the input array objects
input.forEach(object => {
  // For each object, access its `stores` property and 
  //  push its contents into `result[0].stores`
  result[0].stores.push(...object.stores);
})
console.log(result);

相关问题