首页 > 解决方案 > Value 与 Prototype 的比较逻辑

问题描述

所以,有一个“问题”:
你有

let kind_1_ = "some_string"
let kind_2_ = 1234
let kind_3_ = [ "can be string or...", 0, { "some_prop" : "any possible type of value" }, ["...or this"] ]
let kind_4_ = { "even_this" : "will be appropriate" }

const prototype = [ "string", 0, [], {} ]
/* as well as   = [ "string", 0, [] ]       */
/* or as        = [ 0, {} ]                 */
/* and so on... but in most cases with this
   | 4 particular types                     */

因此,我们必须将kind原型进行比较。当时只给出了1种1个原型。我的代码就像这样简单:

let this_will_be_called = (kind, proto) => {
    let bool_to_return = false
    for (let each in proto)
    {
        if (typeof(kind) == typeof(proto)) {
            bool_to_return = true
            return bool_to_return
        }
    }
    return bool_to_return
}

this_will_be_called(kind_any_of_kinds, prototype)

当然,它没有按应有的方式工作。一个主要问题是您无法有效地将 [] 与 {} 进行比较,因为它们都是Object类型(我知道 Array.isArray())。但在以下情况下,它必须返回false

let kind = []
var prototype = [.., {}] /* without [] in here */

/* or this */
let kind = {}
var prototype = [.., []] /* without {} in here */

/* but must return true if: */
let kind = {} /* or let kind = [] */
var prototype = [.., [], {}]

最后,如果没有匹配的类型,它必须返回false 。这个伪代码
解释了我的逻辑:

FOR every element in prototype:
    IF (type of element == type of kind) and (element is NOT Array):
        return TRUE
    ELSE IF (type of element == type of kind) and (element is Array):
        IF (type of kind is NOT an Object) and (kind is Array):
            return TRUE
        ELSE IF (type of kind is an Object):
            return FALSE
    ELSE IF (type of element == type of kind) and (element is Object) and (element is NOT an Array):
        IF (kind is NOT an Array):
            return TRUE
        ELSE IF (kind is an Array):
            return FALSE
    ELSE:
        return FALSE

现在,我需要你的注意力来审核我的逻辑,因为我在解决这个问题时已经完全崩溃了。即使上述逻辑有效,它也不是一个优雅的解决方案。因此,您可以对其进行修改以获得更好的性能。

我对每一个解决方案的赞赏!;)

标签: javascriptlogic

解决方案


首先,你prototype是一个数组,所以当你for..in得到数组的索引而不是项目时,你需要添加类似的东西each = proto[each];

比,当涉及到检测数组与对象时,您可以检测是否kindeach数组比较它们并返回是否相同,如果没有数组,则像以前一样继续使用typeof.

let this_will_be_called = (kind, proto) => {
    let bool_to_return = false
    for (let each in proto)
    {
        each = proto[each];

        const isKindArray = Array.isArray(kind),
              isEachArray = Array.isArray(each);

        if (isKindArray || isEachArray)
        {
          if (isKindArray && isEachArray)
            return true;
        }
        else if (typeof(kind) == typeof(each)) {
            bool_to_return = true
            return bool_to_return
        }
    }
    return bool_to_return
}

let kind = []
let prototype = [ "string", 0, {} ] /* without [] in here */
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

/* or this */
kind = {}
prototype = [ "string", 0, [] ] /* without {} in here */
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

/* but must return true if: */
kind = {} /* or let kind = [] */
prototype = [ "string", 0, [], {} ]
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)


/* as well as   = [ "string", 0, [] ]       */
/* or as        = [ 0, {} ]                 */
/* and so on... but in most cases with this
   | 4 particular types                     */

kind= "some_string"
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

kind = 1234
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

kind = [ "can be string or...", 0, { "some_prop" : "any possible type of value" }, ["...or this"] ]
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

kind = { "even_this" : "will be appropriate" }
console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)


推荐阅读