动态减少javascript map change key
Reduce javascript map change key dynamically
我想在 reduce 函数中使用输入数组输出数组。
但是我无法设法动态添加这样的键 "${keyOrigin}
": cur.value })
感谢您的帮助! <3
const input = [{
date: '10/03/40922',
origin: 'Angleterre',
value: 39
},
{
date: '10/03/40922',
origin: 'Espagne',
value: 60
},
{
date: '10/03/40922',
origin: 'Argentine',
value: 54
},
{
date: '10/03/40922',
origin: 'Italie',
value: 45
},
{
date: '10/03/40922',
origin: 'Hollande',
value: 40
},
{
date: '10/03/40922',
origin: 'Russie',
value: 110
},
{
date: '10/03/40922',
origin: 'Suisse',
value: 87
},
{
date: '10/03/40922',
origin: 'France',
value: 143
},
{
date: '11/03/40922',
origin: 'Angleterre',
value: 39
},
{
date: '11/03/40922',
origin: 'Espagne',
value: 60
},
{
date: '11/03/40922',
origin: 'Argentine',
value: 54
},
{
date: '11/03/40922',
origin: 'Italie',
value: 45
},
{
date: '11/03/40922',
origin: 'Hollande',
value: 40
},
{
date: '11/03/40922',
origin: 'Russie',
value: 110
},
{
date: '11/03/40922',
origin: 'Suisse',
value: 87
},
{
date: '11/03/40922',
origin: 'France',
value: 143
}
];
const output = [{
date: "10/03/40922",
Angleterre: 39,
Espagne: 60,
Argentine: 54,
Italie: 45,
Hollande: 40,
Russie: 110,
Suisse: 97,
France: 143
},
{
date: "11/03/40922",
Angleterre: 39,
Espagne: 60,
Argentine: 54,
Italie: 45,
Hollande: 40,
Russie: 110,
Suisse: 97,
France: 143
},
];
const reduceByDate = (array) => {
const result = Array.from(
array
.reduce((acc, cur) => {
const key = JSON.stringify(cur.date);
const current = acc.get(key) || {
date: cur.date
};
const keyOrigin = JSON.stringify(cur.origin);
return acc.set(key, { ...current,
"`${keyOrigin}`": cur.value
});
}, new Map())
.values()
);
return result;
};
console.log(reduceByDate(input))
您打算使用 computed property name
{ ...current, [cur.origin]: cur.value })
也不需要字符串化 - 简化 - 如果我多花一点时间,我相信我什至可以摆脱地图:
const reduceByDate = array => [...array
.reduce((acc, cur) => {
const date = cur.date;
const current = acc.get(date) || { date };
return acc.set(date, { ...current, [cur.origin]: cur.value });
}, new Map())
.values()];
const reduceByDate = array => [...array
.reduce((acc, cur) => {
const date = cur.date;
const current = acc.get(date) || { date };
return acc.set(date, { ...current, [cur.origin]: cur.value });
}, new Map())
.values()];
console.log(reduceByDate(input))
<script>
const input = [
{ date: '10/03/40922', origin: 'Angleterre', value: 39 },
{ date: '10/03/40922', origin: 'Espagne', value: 60 },
{ date: '10/03/40922', origin: 'Argentine', value: 54 },
{ date: '10/03/40922', origin: 'Italie', value: 45 },
{ date: '10/03/40922', origin: 'Hollande', value: 40 },
{ date: '10/03/40922', origin: 'Russie', value: 110 },
{ date: '10/03/40922', origin: 'Suisse', value: 87 },
{ date: '10/03/40922', origin: 'France', value: 143 },
{ date: '11/03/40922', origin: 'Angleterre', value: 39 },
{ date: '11/03/40922', origin: 'Espagne', value: 60 },
{ date: '11/03/40922', origin: 'Argentine', value: 54 },
{ date: '11/03/40922', origin: 'Italie', value: 45 },
{ date: '11/03/40922', origin: 'Hollande', value: 40 },
{ date: '11/03/40922', origin: 'Russie', value: 110 },
{ date: '11/03/40922', origin: 'Suisse', value: 87 },
{ date: '11/03/40922', origin: 'France', value: 143 }
];</script>
我想在 reduce 函数中使用输入数组输出数组。
但是我无法设法动态添加这样的键 "${keyOrigin}
": cur.value })
感谢您的帮助! <3
const input = [{
date: '10/03/40922',
origin: 'Angleterre',
value: 39
},
{
date: '10/03/40922',
origin: 'Espagne',
value: 60
},
{
date: '10/03/40922',
origin: 'Argentine',
value: 54
},
{
date: '10/03/40922',
origin: 'Italie',
value: 45
},
{
date: '10/03/40922',
origin: 'Hollande',
value: 40
},
{
date: '10/03/40922',
origin: 'Russie',
value: 110
},
{
date: '10/03/40922',
origin: 'Suisse',
value: 87
},
{
date: '10/03/40922',
origin: 'France',
value: 143
},
{
date: '11/03/40922',
origin: 'Angleterre',
value: 39
},
{
date: '11/03/40922',
origin: 'Espagne',
value: 60
},
{
date: '11/03/40922',
origin: 'Argentine',
value: 54
},
{
date: '11/03/40922',
origin: 'Italie',
value: 45
},
{
date: '11/03/40922',
origin: 'Hollande',
value: 40
},
{
date: '11/03/40922',
origin: 'Russie',
value: 110
},
{
date: '11/03/40922',
origin: 'Suisse',
value: 87
},
{
date: '11/03/40922',
origin: 'France',
value: 143
}
];
const output = [{
date: "10/03/40922",
Angleterre: 39,
Espagne: 60,
Argentine: 54,
Italie: 45,
Hollande: 40,
Russie: 110,
Suisse: 97,
France: 143
},
{
date: "11/03/40922",
Angleterre: 39,
Espagne: 60,
Argentine: 54,
Italie: 45,
Hollande: 40,
Russie: 110,
Suisse: 97,
France: 143
},
];
const reduceByDate = (array) => {
const result = Array.from(
array
.reduce((acc, cur) => {
const key = JSON.stringify(cur.date);
const current = acc.get(key) || {
date: cur.date
};
const keyOrigin = JSON.stringify(cur.origin);
return acc.set(key, { ...current,
"`${keyOrigin}`": cur.value
});
}, new Map())
.values()
);
return result;
};
console.log(reduceByDate(input))
您打算使用 computed property name
{ ...current, [cur.origin]: cur.value })
也不需要字符串化 - 简化 - 如果我多花一点时间,我相信我什至可以摆脱地图:
const reduceByDate = array => [...array
.reduce((acc, cur) => {
const date = cur.date;
const current = acc.get(date) || { date };
return acc.set(date, { ...current, [cur.origin]: cur.value });
}, new Map())
.values()];
const reduceByDate = array => [...array
.reduce((acc, cur) => {
const date = cur.date;
const current = acc.get(date) || { date };
return acc.set(date, { ...current, [cur.origin]: cur.value });
}, new Map())
.values()];
console.log(reduceByDate(input))
<script>
const input = [
{ date: '10/03/40922', origin: 'Angleterre', value: 39 },
{ date: '10/03/40922', origin: 'Espagne', value: 60 },
{ date: '10/03/40922', origin: 'Argentine', value: 54 },
{ date: '10/03/40922', origin: 'Italie', value: 45 },
{ date: '10/03/40922', origin: 'Hollande', value: 40 },
{ date: '10/03/40922', origin: 'Russie', value: 110 },
{ date: '10/03/40922', origin: 'Suisse', value: 87 },
{ date: '10/03/40922', origin: 'France', value: 143 },
{ date: '11/03/40922', origin: 'Angleterre', value: 39 },
{ date: '11/03/40922', origin: 'Espagne', value: 60 },
{ date: '11/03/40922', origin: 'Argentine', value: 54 },
{ date: '11/03/40922', origin: 'Italie', value: 45 },
{ date: '11/03/40922', origin: 'Hollande', value: 40 },
{ date: '11/03/40922', origin: 'Russie', value: 110 },
{ date: '11/03/40922', origin: 'Suisse', value: 87 },
{ date: '11/03/40922', origin: 'France', value: 143 }
];</script>