使用 underscore.js 合并数组
Merge arrays with underscore.js
执行一些 underscore.js 操作后(_.map
、_.each
、_.pluck
和 _.flatten
),我有一个对象数组看起来像这样:
var userArray = [
{id: 123456, username: "bill123", group: "ONE"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123458, username: "jim123", group: "ONE"}
]
我需要创建一个新数组,删除重复项,并计算对象在数组中出现的次数。我能够通过两个单独的 underscore.js 函数获得所需的结果,但在合并这两个结果时遇到了问题。
工作职能如下:
var uniqUsers = _.uniq(taggedUsers, false, function(user) {
return user.id
});
//returns array of unique objects in the same format as above
//[{id: 123457, username: "joe123", group: "TWO"},...]
var userCounts = _.countBy(taggedUsers, "id");
//returns the count for each user in the userArray in a single object
//{123456: 1, 123457: 2, 123458: 1}
返回像这样的对象数组的最佳方法是什么:
{id: 123457, username: "joe123", group: "TWO", count: 2}
我可以向 _.countBy
函数添加额外的字段吗?或者我需要用 _.map
做些什么吗?
如有任何帮助,我们将不胜感激!谢谢
我会在两个 _.each
内完成:
var uniquedUsersWithCount = [],
userIDToCount = {};
_.each(taggedUsers, function (user) {
if (userIDToCount[user.id] !== undefined) {
userIDToCount[user.id] += 1;
} else {
userIDToCount[user.id] = 0;
uniquedUsersWithCount.push(user);
}
});
_.each(uniquedUsersWithCount, function (user) {
var count = userIDToCount[user.id];
if (count !== undefined) {
user.count = count;
}
});
您可以在 userCounts
上使用 map
创建一个新数组,然后您可以对其进行排序。
var userArray = [
{id: 123456, username: "bill123", group: "ONE"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123458, username: "jim123", group: "ONE"}
];
var userCounts = _.countBy(userArray, "id");
var result = _.sortBy(_.map(userCounts, function(count, id) {
var user = _.findWhere(userArray, {id: Number(id)});
return _.extend({}, user, {count: count});
}), 'count');
console.log(result);
结果:
[[object Object] {
count: 1,
group: "ONE",
id: 123456,
username: "bill123"
}, [object Object] {
count: 1,
group: "ONE",
id: 123458,
username: "jim123"
}, [object Object] {
count: 2,
group: "TWO",
id: 123457,
username: "joe123"
}]
执行一些 underscore.js 操作后(_.map
、_.each
、_.pluck
和 _.flatten
),我有一个对象数组看起来像这样:
var userArray = [
{id: 123456, username: "bill123", group: "ONE"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123458, username: "jim123", group: "ONE"}
]
我需要创建一个新数组,删除重复项,并计算对象在数组中出现的次数。我能够通过两个单独的 underscore.js 函数获得所需的结果,但在合并这两个结果时遇到了问题。
工作职能如下:
var uniqUsers = _.uniq(taggedUsers, false, function(user) {
return user.id
});
//returns array of unique objects in the same format as above
//[{id: 123457, username: "joe123", group: "TWO"},...]
var userCounts = _.countBy(taggedUsers, "id");
//returns the count for each user in the userArray in a single object
//{123456: 1, 123457: 2, 123458: 1}
返回像这样的对象数组的最佳方法是什么:
{id: 123457, username: "joe123", group: "TWO", count: 2}
我可以向 _.countBy
函数添加额外的字段吗?或者我需要用 _.map
做些什么吗?
如有任何帮助,我们将不胜感激!谢谢
我会在两个 _.each
内完成:
var uniquedUsersWithCount = [],
userIDToCount = {};
_.each(taggedUsers, function (user) {
if (userIDToCount[user.id] !== undefined) {
userIDToCount[user.id] += 1;
} else {
userIDToCount[user.id] = 0;
uniquedUsersWithCount.push(user);
}
});
_.each(uniquedUsersWithCount, function (user) {
var count = userIDToCount[user.id];
if (count !== undefined) {
user.count = count;
}
});
您可以在 userCounts
上使用 map
创建一个新数组,然后您可以对其进行排序。
var userArray = [
{id: 123456, username: "bill123", group: "ONE"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123457, username: "joe123", group: "TWO"},
{id: 123458, username: "jim123", group: "ONE"}
];
var userCounts = _.countBy(userArray, "id");
var result = _.sortBy(_.map(userCounts, function(count, id) {
var user = _.findWhere(userArray, {id: Number(id)});
return _.extend({}, user, {count: count});
}), 'count');
console.log(result);
结果:
[[object Object] {
count: 1,
group: "ONE",
id: 123456,
username: "bill123"
}, [object Object] {
count: 1,
group: "ONE",
id: 123458,
username: "jim123"
}, [object Object] {
count: 2,
group: "TWO",
id: 123457,
username: "joe123"
}]