Javascript 如何基于一个属性优化JS对象的数组查找
我们有大量的对象:Javascript 如何基于一个属性优化JS对象的数组查找,javascript,jquery,arrays,Javascript,Jquery,Arrays,我们有大量的对象: var englishStudents = [ {StudentId: 1, Name: "John"}, {StudentId: 2, Name: "Jack"}, {StudentId: 3, Name: "Jane"} ]; 只需比较一个属性,即可检查此数组中是否包含其他类似对象 var randomStudent = {StudentId: 1337, Name: "Foo"}; 这就是我所拥有的,似乎它会起作用,但我不认为这是最好的方法
var englishStudents = [
{StudentId: 1, Name: "John"},
{StudentId: 2, Name: "Jack"},
{StudentId: 3, Name: "Jane"}
];
只需比较一个属性,即可检查此数组中是否包含其他类似对象
var randomStudent = {StudentId: 1337, Name: "Foo"};
这就是我所拥有的,似乎它会起作用,但我不认为这是最好的方法
var studentIds = $.map(englishStudents, function (student, index) { return student.StudentId; });
var randomStudentLearnsEnglish = false;
for (var sId in studentIds) {
if (randomStudent.StudentId == sId) {
randomStudentLearnsEnglish = true;
break;
}
}
做这件事的最佳方法是什么?只要做一个散列而不是数组,那么:
var englishStudents = {
1: {StudentId: 1, Name: "John"},
2: {StudentId: 2, Name: "Jack"},
3: {StudentId: 3, Name: "Jane"}
};
然后要检索,只需执行以下操作:
var student = englishStudents[id];
只需执行哈希而不是数组,因此:
var englishStudents = {
1: {StudentId: 1, Name: "John"},
2: {StudentId: 2, Name: "Jack"},
3: {StudentId: 3, Name: "Jane"}
};
然后要检索,只需执行以下操作:
var student = englishStudents[id];
如果确实需要,可以创建进一步的索引方案:
var englishStudents = [
{StudentId: 1, Name: "John"},
{StudentId: 2, Name: "Jack"},
{StudentId: 3, Name: "Jane"}
];
//if doing this a lot of time, take the one time hit of construction and memory
var idToNameMap = createIdToNameMap(englishStudents); //returns {'1': 'John', '2': Jack' , '3': 'Jane'}
var randomStudent = getRandomStudent();
if( idToNameMap[ randomStudent.StudentId] != undefined){ ... }
如果确实需要,可以创建进一步的索引方案:
var englishStudents = [
{StudentId: 1, Name: "John"},
{StudentId: 2, Name: "Jack"},
{StudentId: 3, Name: "Jane"}
];
//if doing this a lot of time, take the one time hit of construction and memory
var idToNameMap = createIdToNameMap(englishStudents); //returns {'1': 'John', '2': Jack' , '3': 'Jane'}
var randomStudent = getRandomStudent();
if( idToNameMap[ randomStudent.StudentId] != undefined){ ... }
您应该将学生数据保存在哈希表中,而不是数组中。对于mor复杂场景,您可以维护多个哈希表,如
studentsById
、studentsByCountryCode
,等等。您应该将学生数据保存在哈希表中,而不是数组中。对于mor复杂场景,您可以维护多个哈希表,如studentsById
,studentsByCountryCode
,等等。如果您只想知道ID是否存在,可以执行以下操作:
function checkIdExists( id){
/* map array of matching ID, if none exists length of array is zero*/
return $.map(englishStudents, function (student, index) {
return student.StudentId==id;
}).get().length;
});
使用:
如果您只想知道ID是否存在,可以执行以下操作:
function checkIdExists( id){
/* map array of matching ID, if none exists length of array is zero*/
return $.map(englishStudents, function (student, index) {
return student.StudentId==id;
}).get().length;
});
使用:
这似乎足够有效。是否存在性能问题?数组是否已排序?它们是在一个数组中,而不是在由ID键入的东西中,这有什么原因吗?@elclanrs:我一直在寻找将其中的两个循环变成一个循环的方法。@DaveNewton:这是一个未排序的数组。@FloydPink那么,除非你有某种形式的支持索引,否则迭代是唯一的选择。这似乎足够有效。是否存在性能问题?数组是否已排序?它们在一个数组中,而不是在由ID键入的东西中,这有什么原因吗?@Elclandrs:我想让其中的两个循环以某种方式变成一个。@DaveNewton:这是一个未排序的数组。@FloydPink那么除非你有某种形式的支持索引,否则迭代是唯一的选项。你的对象语法无效你的对象语法无效谢谢。这正是我想要的。虽然在看了这个答案()之后,我觉得$.grep()会更快,但也许它们是一样的。谢谢。这正是我想要的。虽然在看了这个答案()之后,我觉得$.grep()会更快,但也许它们是一样的。