【问题标题】:How can I find which key a value is associated to when the keys are all arrays of strings?当键都是字符串数组时,如何找到与哪个键关联的值?
【发布时间】:2021-11-05 18:28:49
【问题描述】:

在我当前的 React/Typescript 项目中,当每个键都是字符串数组时,我希望找出值在哪个键中。当我知道它关联到什么键时,我需要使用它来更改另一个对象。下面的代码试着让它更清晰一点。

const output = {
    a: false,
    b: false,
    c: false,
}

const checkAgainst = {
    a: ["1", "2", "3"],
    b: ["4", "5", "6"],
    c: ["7", "8", "9"]
}

const checkThing = "5"

// change output based on a match here.

return output;

我尝试了几种不同的解决方案,循环遍历 checkAgainst 对象,但似乎没有什么能够正确识别,然后将 (在本例中为 b) 的值更改为 true。如果需要,我愿意使用一些 lodash(因为它已经是项目的一部分),但我无法找到合适的解决方案。

【问题讨论】:

    标签: javascript arrays reactjs typescript lodash


    【解决方案1】:

    您可以使用Object.keys 获取checkAgainst 对象的键,然后使用reduce 循环键并更改output 对象

    let output = {
      a: false,
      b: false,
      c: false,
    };
    
    const checkAgainst = {
      a: ["1", "2", "3"],
      b: ["4", "5", "6"],
      c: ["7", "8", "9"],
    };
    
    const checkThing = "5";
    
    output = Object.keys(checkAgainst).reduce((acc, curr) => {
      acc[curr] = checkAgainst[curr].includes(checkThing);
      return acc;
    }, output);
    
    console.log(output);

    【讨论】:

      【解决方案2】:

      HR01M8055 有一个很好的答案。

      我相信这可能是另一种写法

      checkStore 检查checkAgainst 键的数组中传入的项目(例如:checkThing),然后将同一键的输出设置为true(如果找到匹配项)或false (如果没有找到匹配项)。

      const checkAgainst = {
          a: [1, 2, 3],
          b: [4, 5, 6],
          c: [7, 8, 9]
      };
      
      const output = {
          a: false,
          b: false,
          c: false
      };
      
      //loop through checkAgainst object via its keys to check if any of the keys contain the item
      function checkStore(item) {
      
          for(let key in checkAgainst) {
         
              if(checkAgainst[key].includes(item)){
                  output[key] = true
              } else {
                  output[key] = false
              } 
          }
      
          return output
      };
      
      
      const checkThing = 5;
      
      checkStore(checkThing); //output { a: false, b: true, c: false }
      
      checkStore(1); //output { a: true, b: false, c: false }
      
      checkStore(11); // output { a: false, b: false, c: false }
      

      【讨论】:

        【解决方案3】:

        您可以使用Object.entries() 结合Array.prototype.forEach()Array.prototype.includes() 来设置/更新output 对象key 的值:

        const output = {
          a: false,
          b: false,
          c: false,
        }
        
        const checkAgainst = {
          a: ["1", "2", "3"],
          b: ["4", "5", "6"],
          c: ["7", "8", "9"],
        }
        
        const checkThing = "5"
        
        Object.entries(checkAgainst).forEach(([k, v]) => output[k] = v.includes(checkThing))
        
        console.log(output)

        【讨论】:

          猜你喜欢
          • 2023-04-07
          • 2019-04-30
          • 2016-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-01
          • 2017-07-28
          • 2011-05-02
          相关资源
          最近更新 更多