英文:
From an array of objects, make an object that holds all values of identical keys
问题
我想要输出看起来像这样的内容:
newObj = [
{ foo: [1, 3, 5] },
{ bar: [2, 4, 6] },
];
或者甚至
newObj = [[1, 3, 5], [2, 4, 6]];
我尝试了类似这样的方法:
const newObj = objArray.map(({ foo, bar }) => ( foo, bar ));
英文:
I've seen this problem explained when only needing the values of one key, but not how to do it when wanting the values of multiple keys.
The object has the following structure:
objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];
I want my output to look like this:
newObj = [
{ foo: [1, 3, 5] },
{ bar: [2, 4, 6] },
];
Or even
newObj = [[1, 3, 5], [2, 4, 6]];
I've tried something like this:
const newObj = objArray.map(({ foo, bar }) => ( foo, bar ));
答案1
得分: 1
###逻辑
- 由于您需要监听对象中的键,而这些对象又是数组的一部分,所以您需要多次循环。
- 父循环:对数组进行循环以获取各个对象。
- 子循环:对当前对象的键进行循环。
- 使用这种方法,您可以创建一个哈希映射(hashmap)。如果在此处使用数组,您将不得不基于键进行搜索以推送新值,而哈希映射可以减少这一工作量。
- 最后,您会得到一个带有键和值的单个对象。您可以循环遍历键并制定您的输出。
const objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];
const result = objArray.reduce((acc, item) => {
Object.entries(item).forEach(([key, value]) => {
acc[key] = acc[key] || [];
acc[key].push(value)
})
return acc
}, {})
const finalValue = Object.entries(result).map(([key, value]) => ({ [key]: value }))
console.log(finalValue)
上述是您提供的代码段的翻译。
英文:
###Logic
- Since you need to listen to keys in an object, which in turn is a part of an array, you need multiple loops.
- Parent Loop: On array to get individual object
- Child Loop: Loop on keys of current object
- Using this, you can create a hashmap. If you use array here, you will have to search based on keys to push new value. hashmap reduces this effort.
- At the end, you have a single object with key and values as an array. You can loop over keys and formulate your output
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];
const result = objArray.reduce((acc, item) => {
Object.entries(item).forEach(([key, value]) => {
acc[key] = acc[key] || [];
acc[key].push(value)
})
return acc
}, {})
const finalValue = Object.entries(result).map(([key, value]) => ({ [key]: value }))
console.log(finalValue)
<!-- end snippet -->
答案2
得分: 0
let objArray = [{
foo: 1,
bar: 2
}, {
foo: 3,
bar: 4
}, {
foo: 5,
bar: 6
}];
// 1. 获取键
const keys = Object.keys(objArray[0]);
// 使用键从每个对象中读取值
const output = objArray.reduce((acc, curr) => {
keys.forEach(key => {
// 如果键存在,则更新,否则初始化它
acc[key] = acc[key] ? acc[key].concat(curr[key]) : [curr[key]]
})
return acc
},{})
// 输出您希望的结果。
console.log(output)
console.log(Object.values(output))
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let objArray = [{
foo: 1,
bar: 2
}, {
foo: 3,
bar: 4
}, {
foo: 5,
bar: 6
}];
// 1. Get the keys
const keys = Object.keys(objArray[0]);
// Use the keys to read values from each object
const output = objArray.reduce((acc, curr) => {
keys.forEach(key => {
// if the key exists update, else initialize it
acc[key] = acc[key] ? acc[key].concat(curr[key]) : [curr[key]]
})
return acc
},{})
// Your outputs ow you want them.
console.log(output)
console.log(Object.values(output))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论