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

huangapple go评论70阅读模式
英文:

Merge arrays of objects in json format in javascript

问题

我有一个类似这样的 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"
  }
]

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

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 => {
  item.stores.forEach(store => {
    grouped.push(store);
  });
});

console.log(grouped);

这段代码将会将所有的商店数据合并到 grouped 数组中。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

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

<!-- end snippet -->

答案1

得分: 2

使用 Array#flatMap()

function mergeStores(array) {
  // 将每个元素转换为其 'stores' 数组,然后将它们扁平化
  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))
英文:

Use Array#flatMap():

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

Try it:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

console.config({ maximize: true });

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

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

console.log(mergeStores(input))

<!-- language: lang-html -->

&lt;script src=&quot;https://gh-canon.github.io/stack-snippet-console/console.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: -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);
英文:
const input = [
  {
    stores: [
      {
        name: &quot;My store&quot;,
        location: &quot;NY&quot;,
      },
      {
        name: &quot;Other store&quot;,
        location: &quot;FL&quot;,
      },
      {
        name: &quot;My other store&quot;,
        location: &quot;NY&quot;,
      },
    ],
  },
  {
    stores: [
      {
        name: &quot;Another My store&quot;,
        location: &quot;NY&quot;,
      },
      {
        name: &quot;Some Other store&quot;,
        location: &quot;FL&quot;,
      },
      {
        name: &quot;Secondary store&quot;,
        location: &quot;NY&quot;,
      },
    ],
  },
];

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

huangapple
  • 本文由 发表于 2023年6月5日 23:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定