underscore.js - 从对象列表中嵌入的对象创建唯一项数组
underscore.js - create array of unique items from objects embedded in a list of objects
我一直在尝试使用我正在处理的 underscore.js in a Meteor 项目,但似乎无法弄清楚如何转换一组对象。
对象看起来像这样(大约有 15k,但它们看起来都像这两个):
[{
"_id": "a_0011223344",
"taggedUsers": [{
"id": 1122244453,
"username": "john123"
}],
"field": "ABC"
}, {
"_id": "a_0011223345",
"taggedUsers": [{
"id": 1122244454,
"username": "bill123"
}, {
"id": 1122244455,
"username": "jim123"
}],
"field": "ABC"
}]
每个对象可以有一个或多个 "taggedUsers"
个对象,我需要一个唯一 "taggedUsers.username"
字段的列表。 Meteor 不支持 mongoDB 的 distinct function, so I am trying to use underscore.js instead (per what I've read and the reccomendation in this post)。
在我的服务器端控制台 db.myCollection.distinct("taggedUsers.username")
return 中得到了想要的结果 ["john123", "bill123", "jim123"]
,但我无法在 underscore.js 中复制它。
我一直在尝试 _.each
、_.map
、_.pluck
和 _.uniq
的组合,但都没有成功。我认为它可能与嵌入对象中的字段有关,但我不确定。
理想情况下,我想要 return 一组如下所示的对象:
[{
"id": 1122244453,
"username": "john123",
"field": "ABC"
}, {
"id": 1122244454,
"username": "bill123",
"field": "ABC"
}, {
"id": 1122244455,
"username": "jim123",
"field": "DEF"
}]
只有 taggedUsers.username
、taggedUsers.id
和 field
字段,并删除了所有重复项,但如果我也能像我一样得到一个 taggedUsers.usernames
的数组,我会很高兴在 db.colleciton.distinct()
函数中。
最终,如果知道如何获取基本数组、唯一对象数组(或者甚至可能如何在模板助手中获取 db.collection.distinct()
的结果),那将是一件好事,但任何帮助或如果方向正确,我们将不胜感激!
根据我们的谈话,听起来最好通过方法调用在服务器上计算它。出于教育目的,以下是使用下划线的方法:
// Get an array of docs somehow
var docs = Collection.find().fetch();
var taggedUsers = _.chain(docs)
.map(function(d) {
// Copy the 'field' to each of the tagged users within a doc
_.each(d.taggedUsers, function(user) {
user.field = d.field;
});
return d;
})
.pluck('taggedUsers') // Extract only the tagged users arrays
.flatten() // Flatten them into a single array
.value();
// taggedUsers could look something like:
// [ { id: 1122244453, username: 'john123', field: 'ABC' },
// { id: 1122244454, username: 'bill123', field: 'ABC' },
// { id: 1122244455, username: 'jim123', field: 'ABC' },
// { id: 1122244453, username: 'john123', field: 'ABC' } ]
// Compute a unique lists of tagged users, where the docs are unique by id
var utu = _.uniq(taggedUsers, false, function(user) {return user.id;});
我一直在尝试使用我正在处理的 underscore.js in a Meteor 项目,但似乎无法弄清楚如何转换一组对象。
对象看起来像这样(大约有 15k,但它们看起来都像这两个):
[{
"_id": "a_0011223344",
"taggedUsers": [{
"id": 1122244453,
"username": "john123"
}],
"field": "ABC"
}, {
"_id": "a_0011223345",
"taggedUsers": [{
"id": 1122244454,
"username": "bill123"
}, {
"id": 1122244455,
"username": "jim123"
}],
"field": "ABC"
}]
每个对象可以有一个或多个 "taggedUsers"
个对象,我需要一个唯一 "taggedUsers.username"
字段的列表。 Meteor 不支持 mongoDB 的 distinct function, so I am trying to use underscore.js instead (per what I've read and the reccomendation in this post)。
在我的服务器端控制台 db.myCollection.distinct("taggedUsers.username")
return 中得到了想要的结果 ["john123", "bill123", "jim123"]
,但我无法在 underscore.js 中复制它。
我一直在尝试 _.each
、_.map
、_.pluck
和 _.uniq
的组合,但都没有成功。我认为它可能与嵌入对象中的字段有关,但我不确定。
理想情况下,我想要 return 一组如下所示的对象:
[{
"id": 1122244453,
"username": "john123",
"field": "ABC"
}, {
"id": 1122244454,
"username": "bill123",
"field": "ABC"
}, {
"id": 1122244455,
"username": "jim123",
"field": "DEF"
}]
taggedUsers.username
、taggedUsers.id
和 field
字段,并删除了所有重复项,但如果我也能像我一样得到一个 taggedUsers.usernames
的数组,我会很高兴在 db.colleciton.distinct()
函数中。
最终,如果知道如何获取基本数组、唯一对象数组(或者甚至可能如何在模板助手中获取 db.collection.distinct()
的结果),那将是一件好事,但任何帮助或如果方向正确,我们将不胜感激!
根据我们的谈话,听起来最好通过方法调用在服务器上计算它。出于教育目的,以下是使用下划线的方法:
// Get an array of docs somehow
var docs = Collection.find().fetch();
var taggedUsers = _.chain(docs)
.map(function(d) {
// Copy the 'field' to each of the tagged users within a doc
_.each(d.taggedUsers, function(user) {
user.field = d.field;
});
return d;
})
.pluck('taggedUsers') // Extract only the tagged users arrays
.flatten() // Flatten them into a single array
.value();
// taggedUsers could look something like:
// [ { id: 1122244453, username: 'john123', field: 'ABC' },
// { id: 1122244454, username: 'bill123', field: 'ABC' },
// { id: 1122244455, username: 'jim123', field: 'ABC' },
// { id: 1122244453, username: 'john123', field: 'ABC' } ]
// Compute a unique lists of tagged users, where the docs are unique by id
var utu = _.uniq(taggedUsers, false, function(user) {return user.id;});