简体   繁体   English

JavaScript对象检查键-但忽略大小写

[英]JavaScript object check for key — but ignore case

Normally, I would do it like this 通常我会这样

if (key in obj) {
    //other stuff
}

But I don't actually know that they are capitalized the same. 但是我实际上并不知道它们的大小写相同。 What's the best way to do this? 最好的方法是什么? I would rather not iterate through the whole array, calling .toLowercase on each key in the object, but is that the only option? 我宁愿不遍历整个数组,对对象中的每个键调用.toLowercase ,但这是唯一的选择吗?

Long Code But Faster 长代码但速度更快

var key, keys = Object.keys(obj);
var n = keys.length;
var objArr=[]
while (n--) {
  objArr.push(keys[n].toLowerCase());
}

if (objArr.indexOf(key)!==-1) {
        //other stuff on obj
}

Short Code, Less Efficient 短代码,效率较低

var objTemp = JSON.parse(JSON.stringify(obj).toLowerCase());

Now check in 现在签入

if (key in objTemp) {
    //other stuff on obj
}

Presumably you don't know what the key-casing is because you're getting the objects from a 3rd party source, such as an AJAX call or other script resource load. 大概您不知道密钥框是什么,因为您是从第三方来源获取对象的,例如AJAX调用或其他脚本资源加载。 You can interject a normalization method at that point and only have to loop once per object, making it a static overhead cost. 您可以在此时插入规范化方法,并且每个对象仅需要循环一次,这使其成为静态开销成本。

For example, define a method 例如,定义一个方法

function normalizeObjectKeys(obj) {
  for (key in obj) {
    obj[key.toUpperCase()] = foo[key];
    delete obj[key];
  }
}

Calling it like this in my Chrome javascript console seems to prove it works. 在我的Chrome JavaScript控制台中这样调用它似乎证明它可以工作。

> var foo = { bar: "bar", baZ: "baZ" }
undefined
> foo
Object {bar: "bar", baZ: "baZ"}
> normalizeObjectKeys(foo)
undefined
> foo
Object {BAR: "bar", BAZ: "baZ"}

Then you always check for the uppercase version of the key. 然后,您始终检查密钥的大写版本。 It's not pretty, and will collide if there are different cases of the same key (eg foo = {bar : "bar", Bar : "baz" } could wind up with either value of BAR ), but it might suit your purposes. 它不是很漂亮,如果同一键存在不同的情况(例如foo = {bar : "bar", Bar : "baz" }可能以BAR任何一个值结束),它会发生冲突,但是它可能适合您的目的。 If you really need to know, you could check whether obj[key.toUpperCase] == undefined before setting the value and log an error or throw an exception. 如果您确实需要知道,可以在设置值之前检查obj[key.toUpperCase] == undefined并记录错误或引发异常。

Here's what you want: 这就是您想要的:

function lowerCaseObjectKeys(object) {
  // clone the original object so that we don't modify its state
  const newObject = Object.assign({}, object);

  Object.keys(newObject).forEach((key) => {
    // if key is already lower-case, skip it & move on!
    if (key.toLowerCase() === key) {
      return;
    }

    // set a new, lower-cased key in newObject
    newObject[key.toLowerCase()] = newObject[key];
    // delete the original, non-lower-cased key (& value) from newObject
    delete newObject[key];
  });

  return newObject;
}

Now, you can fetch a value from an object, regardless of the casing of the key: 现在,无论键的大小写如何,您都可以从对象中获取值:

lowerCaseObjectKeys( yourObjectHere )[ 'lowercasedkeyhere' ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM