javascript-如何检查来自两个json数据的比较数据?
作者:互联网
我用于显示特定帖子详细信息的json数据是
http://127.0.0.1:8000/listings/
{"data": {"pid": 109, "name": "Labs", "website": "service.com", "status": true}
我的JSON数据(用于获取完整的个人资料详细信息)是
http://127.0.0.1:8000/profile/
{"status": true, "data": {"basic": {"name": "Kuis", "is_new": false, "is_email_verified": false, "is_phone_verified": false}, "listings": [{"pid": 109, "nams": "Labs","created_at": "2018-02-14", "validity": "2019-06-29", "views": 2, "gstno": "09485481072", "is_featured": false },{"pid": 112, "nams": "Labs12","created_at": "2018-02-15", "validity": "2019-06-29", "views": 2, "gstno": "09499481072", "is_featured": false }], "total_listings": 2}}
所以,现在我给一个编辑选项.因此,我需要检查pid是否在配置文件数据中退出?我怎样才能达到同样的目的?
我的Vue JS代码是
mounted() {
var self = this;
data = {};
data['auth-token'] = this.authType;
$.ajax({
url: "http://127.0.0.1:8000/profile/",
data: data,
type: "POST",
dataType: 'json',
success: function (e) {
if (e.status == 1) {
self.listings = e.data.listings;
}
},
});
data = {};
data['auth-token'] = this.authType;
data['pid'] = this.pid;
$.ajax({
url: "http://127.0.0.1:8000/listings/",
data: data,
type: "POST",
dataType: 'json',
success: function (e) {
if (e.status == 1) {
self.data = e.data;
}
},
});
},
我在data []中有列表数据,而在profiles []中有配置文件中的所有列表数据.
我需要比较list []中存在的data []中的pid.
我需要在HTML端实现以下功能
<div if="data.pid == listings.pid"> Success </div> <div v-else> Failure</div>
我是一个初学者,只了解基本知识.请帮我解决.
解决方法:
由于清单是一个数组而不是一个对象,因此您需要使用index对其进行循环:
function checkOccurence(data, listing) {
let dataFound = false;
for(let i = 0; i < listing.length; i++) {
if (listing[i].pid === data.pid) {
dataFound = true;
break;
}
}
return dataFound;
};
然后,您可以在渲染器中使用返回的布尔值来确定成功或失败,如下所示:
< div if =“ checkOccurence(data,listing)”>成功< / div> < div v-else>失败< / div>
标签:vue-router,vue-js,vue-component,html,javascript 来源: https://codeday.me/bug/20191109/2012888.html