检查对象已经美元阵列p $ psent? [英] Check if object is already present in array?
问题描述
我的数据的一个巨大的数组的工作,对于这个问题,我会写类似避免所有的键/值的东西。
有是对象数组:
[
{ID:0,名称:'汤姆',年龄:18},
{ID:1,名称:罗布,年龄:22},
{ID:2,名称:卡尔,年龄:'19'},
...
]
有时候用户的添加或更新,并通过上证所,我将收到一个用户对象的响应。
我需要的是检查,如果用户已经是阵列由 ID
签到了。因为如果用户已添加我需要做一些动作,但如果是刚刚更新了我需要做一些别人...
基本上,我需要的是这样的:如果user.id是array.user做一些事情,否则做其他的事情...
我试过到目前为止是一个for循环,但我不认为这是一个好主意,或者我用很糟糕。
使用Array.prototype.filter
您可以使用 Array.prototype.filter
,如提到了这个问题,其他。
变种人= [\r
{ID:0,名称:'汤姆',年龄:18},\r
{ID:1,名称:罗布,年龄:22},\r
{ID:2,名称:卡尔,年龄:'19'}\r
];\r
\r
功能personExists(ID){\r
返回!! people.filter(功能(人){\r
返回person.id == ID;\r
})。长度;\r
}\r
\r
document.body.innerHTML = personExists(2)//真\r
+'< BR>'\r
+ personExists(5); //假
\r
但是该方法将通过所有项目循环,即使该人是从一开始就发现
使用一个循环
要避免这种情况,你可以使用一个很好的旧循环:
变种人= [\r
{ID:0,名称:'汤姆',年龄:18},\r
{ID:1,名称:罗布,年龄:22},\r
{ID:2,名称:卡尔,年龄:'19'}\r
];\r
\r
功能personExists(ID){\r
对于(VAR I = 0; I< people.length;我++){\r
如果(人[I] .ID == ID)返回true;\r
}\r
返回false;\r
}\r
\r
document.body.innerHTML = personExists(2)//真\r
+'< BR>'\r
+ personExists(5); //假
\r
使用对象属性的名称为ID的
另一种方法是提高性能,但会要求您更改数组的对象:
变种人= {\r
0:{名称:'汤姆',年龄:18},\r
1:{名称:'抢',年龄:22},\r
2:{名称:'卡尔',年龄:'19'}\r
};\r
\r
功能personExists(ID){\r
返回people.hasOwnProperty(ID);\r
}\r
\r
document.body.innerHTML = personExists(2)//真\r
+'< BR>'\r
+ personExists(5); //假
\r
I'm working with a huge Array of data, for this question I will write something similar avoiding all the key / value.
There is an Array of Objects:
[
{ id: 0, name: 'Tom', age: '18' },
{ id: 1, name: 'Rob', age: '22' },
{ id: 2, name: 'Carl', age: '19' },
...
]
Sometimes the user is added or updated and via SSE and I receive back a response of that user object.
What I need is to check if the user is already in the array checking by id
. Because if the user has been added I need to do a few actions but if it is just updated I need to do a few others...
Basically what I need is something like: if user.id is in array.user do something, else do something else...
What I tried so far is a for loop, but I don't think it is a good idea, or maybe I used it badly.
Using Array.prototype.filter
You can use Array.prototype.filter
, as mentioned in this other question.
var people = [
{ id: 0, name: 'Tom', age: '18' },
{ id: 1, name: 'Rob', age: '22' },
{ id: 2, name: 'Carl', age: '19' }
];
function personExists(id){
return !!people.filter(function(person){
return person.id == id;
}).length;
}
document.body.innerHTML = personExists(2) // true
+ '<br>'
+ personExists(5); // false
But that method would loop through all items, even if the person is found from the start.
Using a loop
To avoid this, you could use a good old loop:
var people = [
{ id: 0, name: 'Tom', age: '18' },
{ id: 1, name: 'Rob', age: '22' },
{ id: 2, name: 'Carl', age: '19' }
];
function personExists(id){
for(var i=0; i<people.length; i++){
if(people[i].id == id) return true;
}
return false;
}
document.body.innerHTML = personExists(2) // true
+ '<br>'
+ personExists(5); // false
Using Object property names as IDs
Another method would enhance performance, but would require you to change your Array into an object:
var people = {
'0' : { name: 'Tom', age: '18' },
'1' : { name: 'Rob', age: '22' },
'2' : { name: 'Carl', age: '19' }
};
function personExists(id){
return people.hasOwnProperty(id);
}
document.body.innerHTML = personExists(2) // true
+ '<br>'
+ personExists(5); // false
这篇关于检查对象已经美元阵列p $ psent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!