skip to Main Content

I have a json file like this:

[
  {
    "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"
         }
     ],
  }
]

I want to combine the data so it is :

[
        {
          "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"
         }
]

I started with a loop but I dont know how can I group all of them into 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)
})

2

Answers


  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",
          },
        ],
      },
    ];
    
    // 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);
    
    Login or Signup to reply.
  2. Use Array#flatMap():

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

    Try it:

    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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search