簡體   English   中英

如何檢查變量是否以特定格式定義?

[英]How to check whether a variable is defined in a specific format?

假設我有一個模板變量測試。

test = {
    key1: "String",
    key2: {
        key1: Int,
        key2: ["Array","of","Strings"]
    },
    key3: [0,1,2...],   // Array of Integers
    key4: true          // Boolean value 
}

現在,我要檢查變量是否具有與該模板完全相同的“格式”,這意味着它需要具有4個鍵,一個鍵必須是字符串,一個鍵必須是包含2個鍵的對象(Int和Array)字符串),一個鍵必須是整數數組,一個鍵必須具有布爾值。

例如,此變量不是test模板變量的格式。

example = {
    key1: "String",
    key2: "String",
    key3: "String"
}

有沒有一種方法可以將一個變量的結構與另一個變量進行比較,以確保其格式正確,例如可以使用它向服務器發送AJAX調用而不會出現格式問題,從而使用正確的格式?

注意 :template變量只是一個示例,它可以具有任何格式,其中包含任意數量的嵌套對象。

您可以僅對屬性進行比較,然后調用該函數並檢查屬性類型是否匹配。

根據評論,我將遞歸添加到更深層次。

您還必須檢查對象是否為數組。 我將把這個Array.isArray(value)留給您,以將其放置在正確的位置。

 let basething = { key1: "String", key2: { key1a: 4, key2arr: ["Array", "of", "Strings"] }, key3arr: [0, 1, 2], // Array of Integers key4: true // Boolean value }; let test = { key1: "String", key2: { key1a: 98, key2arr: ["Array", "of", "Strings", "not exact"] }, key3arr: [0, 1, 2, 5, 6, 123], // Array of Integers key4: true // Boolean value }; let example = { key1: "String", key2: "String", key3: "String" } function compareProperty(object1, object2) { let sameStruct = true; for (let p in object1) { console.log(p,typeof object1[p]); // compare property if (!object2.hasOwnProperty(p)) { sameStruct = false; } else { if (object2.hasOwnProperty(p)) { if (typeof object1[p] == "object" && typeof object2[p] == "object") { sameStruct = sameStruct && compareProperty(object1[p], object2[p]); } else { // compare type of property sameStruct = sameStruct && (typeof object2[p] === typeof object1[p]); } } else { sameStruct = false; } } } return sameStruct; } let isSame = compareProperty(basething, example); let isSame2 = compareProperty(basething, test); console.log(isSame, isSame2); 

您可以創建每個都帶有一個參數的函數數組,並為每個給定條件返回truefalse 在給定對象的Object.values上使用every() ,並檢查任何函數是否對該值返回true。

 const test = { key1: "String", key2: { key1: 55, key2: ["Array","of","Strings"] }, key3: [0,1,2], // Array of Integers key4: true // Boolean value } let cons = [ x => x.constructor === String, x => x.constructor === Array && x.every(a => typeof a === "number"), x => x.constructor === Boolean, x => { if(x.constructor === Object){ let funs = [ (x) => x.constructor === Number, (x) => x.every(x => x.constructor === String) ] return testFormat(x,funs) } } ] function testFormat(obj, funs){ let vals = Object.values(obj); return vals.length === funs.length && vals.every(x => funs.some(f => f(x))); } console.log(testFormat(test, cons)) 

我認為https://validatejs.org/可以為您提供幫助。

Sometimes it's nice to be able validate field differently depending on the input itself. validate.js allows the validators object and validator options to be a function that should return the constraints/options:

var constraints = {
  username: {
    presence: true,
    exclusion: {
      within: ["nicklas"],
      message: "'%{value}' is not allowed"
    }
  },
  password: {
    presence: true,
    length: {
      minimum: 6,
      message: "must be at least 6 characters"
    }
  }
};

validate({password: "bad"}, constraints);
// => {
//   "username": ["Username can't be blank"],
//   "password": ["Password must be at least 6 characters"]
// }

https://validatejs.org/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM