[英]How to check if an id value exists in an object array
Given an array "posts" of objects {id: "x", title: "title"}, i need to verify whether there is a previously entered id before entering a new post (via form in which id is a field). 给定对象{id:“ x”,title:“ title”}的数组“ posts”,我需要在输入新帖子 (通过id是字段的表单) 之前验证是否存在先前输入的id 。
I tried looping through posts, but it causes problems on entering repeated new data. 我尝试遍历帖子,但是在输入重复的新数据时会引起问题。 Thank you for your help, this is driving me mad.
谢谢您的帮助,这让我发疯了。
// ADD NEW POST FROM FORM TO ARRAY
$("#form").submit(function(event) {
event.preventDefault();
var id = Number($("#idPost").val());
var title = $("#titlePost").val();
var date = new Date($("#data").val());
// CHECK IF id ALREADY EXISTS, IF YES, BLOCK ENTRY
for(num of posts){
console.log (id, num.id);
if(id === num.id){
console.log("error");
} else {
var post = new Post(id, title, date);
}
};
});
As @Andreas suggested, you can use Array.Some()
to find element exist in array or not. 正如@Andreas建议的那样,您可以使用
Array.Some()
查找数组中是否存在元素。
var found = posts.some(el => el.id === id ); // It returns true if id exist in an array
if(found)
{
//Your codes here
}
Or you can try with Array.Filter()
to find element is exist or not in array. 或者,您可以尝试使用
Array.Filter()
来查找元素是否存在于数组中。
var found = posts.filter(el => el.id === id).length > 0; // .length returns 1 if id exist in an array
if(found)
{
//Your code goes here
}
You can try Array.find()
or Array.IndexOf()
to perform same operation 您可以尝试
Array.find()
或Array.IndexOf()
执行相同的操作
Following implementation with Array.Some()
以下使用
Array.Some()
//Lets consider below is the value in posts arry var posts = [ { id: 1, username: 'foo' },{ id: 2, username: 'bar' } ]; var newObj = {"id": 3, "username": 'prasad'}; //console.log(posts); var found = posts.some(el => el.id === 2 ); if(found) { for(num of posts){ //console.log (3, num.id); if(4 === num.id){ //Your code //console.log("error"); } else { //Your code //console.log(num); } }; posts.push(newObj); console.log(posts); }
You can use either Array.prototype.some() as Andreas suggested or use Array.prototype.find() . 您可以按照Andreas的建议使用Array.prototype.some()或使用Array.prototype.find() 。
Array.prototype.some() will just return true or false and will not return the matching object. Array.prototype.some()只会返回true或false,不会返回匹配的对象。 If you need the object, use find() as follows:
如果需要该对象,请使用find(),如下所示:
$("#form").submit(function(event) {
event.preventDefault();
var id = Number($("#idPost").val());
var title = $("#titlePost").val();
var date = new Date($("#data").val());
var objectPresent = posts.find(function(post) {
return post.id === id;
});
if(!objectPresent) {
var post = new Post(id, title, date);
} else {
console.log(objectPresent.id,"is already taken up by ", objectPresent.title);
}
}); });
I think you should use the JSON object, it will help you to identify easily, that how to identify, is key present in JSON object list or not 我认为您应该使用JSON对象,它将帮助您轻松识别,如何识别,密钥是否存在于JSON对象列表中
JSON_Object.hasOwnProperty(ID)
var x = {};
x[1] = "{'title1':'title1'}";
x[2] = "{'title2':'title2'}";
x[3] = "{'title3':'title3'}";
if(x.hasOwnProperty(1)){
console.log('value present');
}
if(x.hasOwnProperty(5)){
console.log('value not present');
}
This may help you. 这可能对您有帮助。
This is just a way to resolve your problem. 这只是解决问题的一种方法。 Use this, thanks me later, cheers :)
使用这个,以后再谢谢我,:)
This should work: 这应该工作:
if (postExists(posts, id)){
// invlaid post entry
} else {
// add new post
}
function postExists(posts, id) {
return posts.some(function(post){
return post.id == id;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.