【问题标题】:trouble with iterating through values in key/value pairs遍历键/值对中的值时遇到问题
【发布时间】:2022-01-05 17:09:45
【问题描述】:

我编写了一个搜索对象值的函数。确认键与数组中存储的值的索引匹配时返回键。

例如。 19 在数组的索引 0 中,而在对象中,19 的键为 0。由于匹配,因此返回键。

但是,如果在对象中找到值但在数组中的索引错误,则返回-2。最后,当在对象中没有找到值时,返回-1。

很遗憾,我的功能没有按预期工作。我有一种感觉,该函数没有将对象中的值识别为与数组中相同的数据类型,因此我使用 == 而不是 === 进行比较,但结果是相同的。

const exampleObject = {
   0: 19,
   1: 20,
   2: 21,
   3: 22
};


function getKey(array, object, value) {
    for (const prop in object) {
        if (object.hasOwnProperty(prop)) {
            if (object[prop] === value && array[object[prop]] == value ){
              return prop
            }
            else if (object[prop] === value && array[object[prop]] !== value ){
              return -2
            }
        }
    }
    return -1
}

console.log(getKey([19,20,22,21],exampleObject,20)) //returns -2 which is wrong as the index is correct

console.log(getKey([19,20,22,21],exampleObject,25)) //returns -1 which is correct

console.log(getKey([19,20,22,21],exampleObject,22)) //returns -2 which is correct

console.log(getKey([19,20,22,21],exampleObject,19)) //returns -2 which is wrong as the index is correct

编辑:我使用了该站点的代码并对其进行了一些调整: https://www.geeksforgeeks.org/how-to-get-a-key-in-a-javascript-object-by-its-value/

【问题讨论】:

  • if (object.hasOwnProperty(prop)) { 永远是真的,对吧?
  • @0stone0 是的。老实说,我不太确定它的重要性。我复制了这段代码并从这个站点geeksforgeeks.org/… 进行了一些修改
  • 对象的键是否总是数字,从 0 开始,以 1 递增?

标签: javascript arrays object


【解决方案1】:

您只需要使用array[prop] 而不是array[object[prop]],因为object[prop] 将返回值,但您需要密钥。

const exampleObject = {
  0: 19,
  1: 20,
  2: 21,
  3: 22
};

function getKey(array, object, value) {
  for (const prop in object) {
    if (object.hasOwnProperty(prop)) {
      if (object[prop] === value && array[prop] === value) {
        return prop
      } else if (object[prop] === value && array[prop] !== value) {
        return -2
      }
    }
  }
  return -1
}

console.log(getKey([19, 20, 22, 21], exampleObject, 20))
console.log(getKey([19, 20, 22, 21], exampleObject, 25))
console.log(getKey([19, 20, 22, 21], exampleObject, 22))
console.log(getKey([19, 20, 22, 21], exampleObject, 19))

【讨论】:

    【解决方案2】:

    如果您的对象总是使用0 - n 作为键,这可以简化很多。

    所以,我们假设对象总是有数字键,从0 开始,以1 递增。

    在这种情况下,我们可以定义一个辅助数组,其中包含对象的所有值,如下所示:

    const exampleObjectValues = Object.values(exampleObject);
    

    现在,我们可以使用以下逻辑创建一个简单的函数;

    1. 如果exampleObjectValues不包含value,则返回-1
    2. 否则,
      1. 从输入数组中获取索引
      2. exampleObjectValues获取索引
      3. 比较它们并:
        1. 如果匹配,则返回 objectValueIndex(步骤 2.1)
        2. 如果不匹配,返回-2

    const exampleObject = {
       0: 19,
       1: 20,
       2: 21,
       3: 22
    };
    const exampleObjectValues = Object.values(exampleObject);
    
    function getKey(array, value) {
        if (!exampleObjectValues.includes(value)) {
          return -1;
        } else {
          const arrayIndex = array.indexOf(value);
          const objectValueIndex = exampleObjectValues.indexOf(value);
          return (arrayIndex !== objectValueIndex) ? -2 : objectValueIndex;
        }
    }
    
    console.log(getKey([19,20,22,21] ,20)); // 1 
    console.log(getKey([19,20,22,21], 25)); // -1 
    console.log(getKey([19,20,22,21], 22)); // -2
    console.log(getKey([19,20,22,21], 19)); // 0

    上面会输出:

    1
    -1
    -2
    0
    

    【讨论】:

    • 但这不会循环两次值吗?这是可以预防的。并且数组也循环了一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多