映射到 Javascript 数组,以计算不同的值
Mapping over a Javascript array, to count distinct values
我正在努力寻找映射这组记录并将计数附加到对象本身的方法。这是一个示例数据列表,我需要 return 最后的 3 个用户中的每一个,但是我还需要 return 计数。例如,来自下面响应的 returned 数据应该像这样附加 user_song_count。
{
user_song_count: 2,
user_song: {
user_id: 2,
username: 'tommy.g',
}
示例数据
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 1,
username: 'billy.m',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 3,
username: 'sally.e',
},
user_time: null,
user_scene: null,
}
let sample_data = [{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 1,
username: 'billy.m',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 3,
username: 'sally.e',
},
user_time: null,
user_scene: null,
}];
let result = sample_data.map ( function (x) {
x.user_song_count = sample_data.filter ( y => y.user_song.user_id == x.user_song.user_id ).length;
return x;
});
console.log (result);
您可以使用Array.prototype.reduce合并具有相似user_id
的对象。
const data = [
{
user_song: { user_id: 2, username: "tommy.g" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 1, username: "billy.m" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 2, username: "tommy.g" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 3, username: "sally.e" },
user_time: null,
user_scene: null,
},
];
const result = Object.values(
data.reduce((r, d) => {
if (!r[d.user_song.user_id]) {
r[d.user_song.user_id] = { ...d, user_song_count: 0 };
}
r[d.user_song.user_id].user_song_count += 1;
return r;
}, {})
);
console.log(result);
其他文档:
我正在努力寻找映射这组记录并将计数附加到对象本身的方法。这是一个示例数据列表,我需要 return 最后的 3 个用户中的每一个,但是我还需要 return 计数。例如,来自下面响应的 returned 数据应该像这样附加 user_song_count。
{
user_song_count: 2,
user_song: {
user_id: 2,
username: 'tommy.g',
}
示例数据
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 1,
username: 'billy.m',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 3,
username: 'sally.e',
},
user_time: null,
user_scene: null,
}
let sample_data = [{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 1,
username: 'billy.m',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 2,
username: 'tommy.g',
},
user_time: null,
user_scene: null,
},
{
user_song: {
user_id: 3,
username: 'sally.e',
},
user_time: null,
user_scene: null,
}];
let result = sample_data.map ( function (x) {
x.user_song_count = sample_data.filter ( y => y.user_song.user_id == x.user_song.user_id ).length;
return x;
});
console.log (result);
您可以使用Array.prototype.reduce合并具有相似user_id
的对象。
const data = [
{
user_song: { user_id: 2, username: "tommy.g" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 1, username: "billy.m" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 2, username: "tommy.g" },
user_time: null,
user_scene: null,
},
{
user_song: { user_id: 3, username: "sally.e" },
user_time: null,
user_scene: null,
},
];
const result = Object.values(
data.reduce((r, d) => {
if (!r[d.user_song.user_id]) {
r[d.user_song.user_id] = { ...d, user_song_count: 0 };
}
r[d.user_song.user_id].user_song_count += 1;
return r;
}, {})
);
console.log(result);
其他文档: