如何根据 Javascript 中的两个键构建查找
How to structure a lookup based on two keys in Javascript
我有超过一万个对象,它们代表两个 ids
的一些信息,看起来像:
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
];
我目前正在使用 lodash 查找与两个 ids
相匹配的一个对象,例如:
function findData(id1, id2) {
return _.filter(muchData, function(d) {
return d.ids.indexOf(id1) > -1 && d.ids.indexOf(id2) > -1
})
}
如果不能保证我将收到 id1 和 id2 的顺序(即 ids
数组中的第一个值可以是 id1 或 id2)。
是否有更好的方法来表示此问题以避免每次查找都必须过滤整个 muchData
数组?
你可以取一个散列table。使用唯一键的排序 ID。
var muchData = [{ ids: ["123", "234"], interestingData: 1 }, { ids: ["123", "345"], interestingData: 2 }, ],
hash = muchData.reduce(function (r, a, i) {
var k = a.ids[0] < a.ids[1] ? a.ids[0] + '|' + a.ids[1] : a.ids[1] + '|' + a.ids[0];
r[k] = r[k] || [];
r[k].push(a);
return r;
}, {});
document.write('<pre>' + JSON.stringify(hash, 0, 4) + '</pre>');
最初是一个(冗长的)评论,稍微扩展为一个答案。
鉴于数组的性质:
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
];
如果,如你在中所说:
The ids are guaranteed to be unique.
那么最简单的方法是使用组合的 id
属性 值作为数组的索引:
var sortedData = [],
muchData.forEach(function (obj, index, array) {
sortedData[ parseInt( obj.id.join(''), 10) ] = obj.interestingData;
});
然后使用创建的数组搜索您要检索的 interestingData
。这样做的好处是它只需要发生一次(每次客户访问),当然,这也可以在服务器端完成(一次)以使其更容易。
或者,您可以将数组转换为对象并使用组合的 id
属性作为键(这可能比创建一个包含很多 empty/undefined 个条目):
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
],
objOfData = {},
muchData.forEach(function (obj, index, array) {
objOfData[ obj.id.join('') ] = obj;
});
我有超过一万个对象,它们代表两个 ids
的一些信息,看起来像:
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
];
我目前正在使用 lodash 查找与两个 ids
相匹配的一个对象,例如:
function findData(id1, id2) {
return _.filter(muchData, function(d) {
return d.ids.indexOf(id1) > -1 && d.ids.indexOf(id2) > -1
})
}
如果不能保证我将收到 id1 和 id2 的顺序(即 ids
数组中的第一个值可以是 id1 或 id2)。
是否有更好的方法来表示此问题以避免每次查找都必须过滤整个 muchData
数组?
你可以取一个散列table。使用唯一键的排序 ID。
var muchData = [{ ids: ["123", "234"], interestingData: 1 }, { ids: ["123", "345"], interestingData: 2 }, ],
hash = muchData.reduce(function (r, a, i) {
var k = a.ids[0] < a.ids[1] ? a.ids[0] + '|' + a.ids[1] : a.ids[1] + '|' + a.ids[0];
r[k] = r[k] || [];
r[k].push(a);
return r;
}, {});
document.write('<pre>' + JSON.stringify(hash, 0, 4) + '</pre>');
最初是一个(冗长的)评论,稍微扩展为一个答案。
鉴于数组的性质:
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
];
如果,如你在
The ids are guaranteed to be unique.
那么最简单的方法是使用组合的 id
属性 值作为数组的索引:
var sortedData = [],
muchData.forEach(function (obj, index, array) {
sortedData[ parseInt( obj.id.join(''), 10) ] = obj.interestingData;
});
然后使用创建的数组搜索您要检索的 interestingData
。这样做的好处是它只需要发生一次(每次客户访问),当然,这也可以在服务器端完成(一次)以使其更容易。
或者,您可以将数组转换为对象并使用组合的 id
属性作为键(这可能比创建一个包含很多 empty/undefined 个条目):
muchData = [
{
ids: ["123", "234"],
interestingData: 1
},
{
ids: ["123", "345"],
interestingData: 2
},
...
],
objOfData = {},
muchData.forEach(function (obj, index, array) {
objOfData[ obj.id.join('') ] = obj;
});