[英]How to reduce two array to get a custom object
I've got two object arrays with float value and string for shorthanded category.我有两个带有浮点值和字符串的对象数组,用于速记类别。
const summer = [
{ _id: 'cat1', Datasets: 496, Average: 27.586688911290324 },
{ _id: 'catB', Datasets: 237, Average: 27.6677358649789 },
{ _id: 'cat3', Datasets: 15, Average: 25.567020000000001 },
{ _id: 'catD', Datasets: 51, Average: 24.998184313725492 }
]
const winter = [
{ _id: 'cat1', Datasets: 496, Average: 17.586688911290324 },
{ _id: 'catB', Datasets: 237, Average: 17.6677358649789 },
{ _id: 'cat3', Datasets: 15, Average: 15.567020000000001 },
{ _id: 'catD', Datasets: 51, Average: 14.998184313725492 }
]
The shorthanded category strings should be 'translated' to the correct category label and the float values should be pushed to the season field.速记的类别字符串应该被“翻译”为正确的类别标签,并且浮点值应该被推送到季节字段。
const target = {
cat1: { label: 'Cars', index: 0 },
catB: { label: 'Planes', index: 1 },
cat3: { label: 'People', index: 2 },
catD: { label: 'Vegetables', index: 3 }
}
So the result for the example data should be:所以示例数据的结果应该是:
{
labels: [
'Cars',
'Planes',
'People',
'Vegetables'
],
season: {
summer: [
27.586688911290324,
24.998184313725492,
27.6677358649789,
25.567020000000001
]
winter: [
17.586688911290324,
14.998184313725492,
17.6677358649789,
15.567020000000001
]
},
title: 'Just a title string'
}
My attempt looks like this, but it handles only one data array:我的尝试看起来像这样,但它只处理一个数据数组:
const result = data.reduce((r, { _id, Average }) => {
r.labels[target[_id].index] = target[_id].label
r.season.winter[target[_id].index] = Average
return r
}, { labels: [], season: { winter: [] }, title: 'Just a title string' })
You could take an object with winter
and summer
as short hand properties and iterate the entries from it.您可以将具有
winter
和summer
的对象作为速记属性并从中迭代条目。
This approach takes the labels first to prevent for every object a new setting.这种方法首先使用标签来防止每个对象都有新的设置。
const getLabels = target => Object.values(target).reduce((r, { label, index }) => { r[index] = label; return r; }, []), summer = [{ _id: 'cat1', Datasets: 496, Average: 27.586688911290324 }, { _id: 'catB', Datasets: 237, Average: 27.6677358649789 }, { _id: 'cat3', Datasets: 15, Average: 25.567020000000001 }, { _id: 'catD', Datasets: 51, Average: 4.998184313725492 }], winter = [{ _id: 'cat1', Datasets: 496, Average: 17.586688911290324 }, { _id: 'catB', Datasets: 237, Average: 17.6677358649789 }, { _id: 'cat3', Datasets: 15, Average: 15.567020000000001 }, { _id: 'catD', Datasets: 51, Average: 14.998184313725492 }], target = { cat1: { label: 'Cars', index: 0 }, catB: { label: 'Planes', index: 1 }, cat3: { label: 'People', index: 2 }, catD: { label: 'Vegetables', index: 3 } }, result = Object .entries({ summer, winter }) .reduce((r, [key, data]) => { r.season[key] = []; data.forEach(({ _id, Average }) => r.season[key][target[_id].index] = Average); return r; }, { labels: getLabels(target), season: { }, title: 'Just a title string' }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const summer = [
{ _id: 'cat1', Datasets: 496, Average: 27.586688911290324 },
{ _id: 'catB', Datasets: 237, Average: 27.6677358649789 },
{ _id: 'cat3', Datasets: 15, Average: 25.567020000000001 },
{ _id: 'catD', Datasets: 51, Average: 24.998184313725492 }
]
const winter = [
{ _id: 'cat1', Datasets: 496, Average: 17.586688911290324 },
{ _id: 'catB', Datasets: 237, Average: 17.6677358649789 },
{ _id: 'cat3', Datasets: 15, Average: 15.567020000000001 },
{ _id: 'catD', Datasets: 51, Average: 14.998184313725492 }
]
const target = {
cat1: { label: 'Cars', index: 0 },
catB: { label: 'Planes', index: 1 },
cat3: { label: 'People', index: 2 },
catD: { label: 'Vegetables', index: 3 }
}
let desiredObj = {
labels: [],
season: {
summer: [],
winter: []
},
title: 'Just a title string'
}
Object.keys(target).forEach(targetKey => {
const targetLabel = target[targetKey].label
const targetIndex = target[targetKey].index
const summerObjFoundByTargetKey = summer.find(obj => obj['_id'] === targetKey)
const winterObjFoundByTargetKey = winter.find(obj => obj['_id'] === targetKey)
desiredObj.labels.push(targetLabel)
if(summerObjFoundByTargetKey) {
desiredObj.season.summer[targetIndex] = summerObjFoundByTargetKey.Average
}
if(winterObjFoundByTargetKey) {
desiredObj.season.winter[targetIndex] = winterObjFoundByTargetKey.Average
}
})
console.log('desiredObj', desiredObj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.