英文:
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 = [
{
"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)
})
<!-- 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 'stores' array, then flat all of them
return array.flatMap(element => 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 => 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))
<!-- language: lang-html -->
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<!-- 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: "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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论