【问题标题】:how to validate object in javascript如何在javascript中验证对象
【发布时间】:2019-03-28 09:42:05
【问题描述】:

我想知道如何通过javascript中的输入对象验证对象是否存在

我有结果对象,应该检查该属性是否存在于其他输入对象中,如果都存在则返回 true 否则为 false

/* result object */
var result_query= {
  send_country: 'Singapore', // check if exist in obj_cn 'country_name'
  sccy: 'SGD', // check if exist in obj_cn 'country-from'
  receive_country: 'India', // check if exist in obj_cn'popular_to'
  rccy: 'INR' // check if exist in obj_ccy 'currency' by using receive_country 
}
/*if all exists return true , if single value doesnot exist return false*/


/* others object */
var paramValue = ["Singapore", "India"];
var obj_cn = [
{
  country_name: "Singapore",
  country_from:["SGD"],
  popular_to: ["India"],
  country_to: ["SGD"],
  country_code: "SG"
},
{
  country_name: "India",
  country_from:["INR"],
  popular_to: ["UnitedStates"],
  country_to: ["USD"],
  country_code: "IN"
}
]
var obj_ccy = [
{
   currency: "SGD",
   country_code: "SG",
   country_name: "Singapore"
}
]

【问题讨论】:

  • 换个域名和行话,能举个简单的例子吗?也显示你尝试过的东西。
  • @sabithpocker 感谢您的及时回复,更新了我使用 javascript 尝试过的内容
  • 什么是输入对象?
  • @MarkBaijens 感谢回复,paramValue, obj_cn, obj_ccy 是输入
  • @MarkBaijens 感谢您的回答,但仍然返回 true,如果 result_obj send_country 是 Singapore 并且 sccy 是 INR ,它应该是 false,根据输入对象,

标签: javascript jquery arrays object


【解决方案1】:

您可以在包含对象的 javscript 数组上使用 filter() 函数,并在仅包含字符串的数组上使用 indexOf()

在 sn-p 中编写代码,但我建议用它制作一个函数。

//Source data objects
var obj_cn = [{
  country_name: "Singapore",
  country_from:["SGD"],
  popular_to: ["India"],
  country_to: ["SGD"],
  country_code: "SG"
}];

var obj_ccy = [{
   currency: "SGD",
   country_code: "SG",
   country_name: "Singapore"
}];

var result_query = {
  send_country: 'Singapore', // check if exist in obj_cn 'country_name'
  sccy: 'INR', // check if exist in obj_cn 'country-from'
  receive_country: 'India', // check if exist in obj_cn'popular_to'
  rccy: 'INR' // check if exist in obj_ccy 'currency' by using receive_country 
};

//city check
var cityFound = obj_cn.filter(function(cn) {
  return cn.country_name == result_query.send_country;
}).length > 0;

//sccy check
var sccyFound = obj_cn.filter(function(cn) {
  return cn.country_from.indexOf(result_query.sccy) != -1;
}).length > 0;

//country check
var countryFound = obj_cn.filter(function(cn) {
  return cn.popular_to.indexOf(result_query.receive_country) != -1;
}).length > 0;

//ccy check
var ccyFound = obj_cn.filter(function(ccy) {
  return ccy.currency == result_query.rccy;
}).length > 0;

//test results
console.log("City found: " + cityFound);
console.log("Sccy found: " + sccyFound);
console.log("Country found: " + countryFound);
console.log("Ccy found: " + ccyFound);
console.log("All found: " + (cityFound && sccyFound && countryFound && ccyFound));

【讨论】:

  • 感谢您的回答,但仍然返回 true,如果 result_obj send_country 是 Singapore 并且 sccy 是 INR ,它应该是 false,根据输入对象,
  • @Senthil 我更新了我的答案,所以result_query.sccyINR,它确实会导致错误。
猜你喜欢
  • 2022-11-21
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 2017-10-20
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多