为什么这个JavaScript对象键不匹配具有相同名称的字符串?

时间:2022-04-10 16:45:02

Here is a basic comparison of a test object.

这是测试对象的基本比较。

var testObj = {"hey":"blue"};
alert(Object.keys(testObj)); // alerts hey
if (Object.keys(testObj) === "hey") {
  alert('matches');
}

The first alert produces "hey", which matches the right side of the if statement comparison "hey" yet the alert('matches') does not fire, why is that?

第一个警报产生“嘿”,它匹配if语句比较的右侧“嘿”但警报('匹配')没有触发,为什么会这样?

edit: sorry dumb question

编辑:抱歉愚蠢的问题

5 个解决方案

#1


2  

"Object.keys(testObj)" return an array. so basically you comparing an array. try below code snippet

“Object.keys(testObj)”返回一个数组。所以基本上你比较一个数组。尝试下面的代码片段

var testObj = {"hey":"blue"};
alert(Object.keys(testObj)); // alerts hey
console.log(Object.keys(testObj));
if (Object.keys(testObj)[0] === "hey") {
  alert('matches');
}

#2


1  

Object.keys() method returns an array of a given object's own enumerable properties.So you need to get the index from that array

Object.keys()方法返回给定对象自己的可枚举属性的数组。所以你需要从该数组中获取索引

var testObj = {
  "hey": "blue"
};
alert(Object.keys(testObj)); // alerts hey
//[0] will be first element in the array
if (Object.keys(testObj)[0] === "hey") {
  alert('matches');
}

#3


1  

You must need to specify index number in order to use it: check this.

您必须指定索引号才能使用它:检查一下。

<script>
function myfn()
{
    var testObj = {"hey":"blue"};
    alert(Object.keys(testObj)); // alerts hey
    if (Object.keys(testObj)[0] === "hey") {
      alert('matches');
    }
}
</script>


<body onload="myfn();"></body>

#4


1  

Object.keys(obj) retuns an array, so if you have multiple keys in the object then you can loop over them to match

Object.keys(obj)返回一个数组,所以如果你在对象中有多个键,那么你可以循环它们来匹配

var testObj = {"hey":"blue", "something": "else"};
console.log(Object.keys(testObj)); // alerts hey
Object.keys(testObj).forEach(function(key) {
   if (key === "hey") {
    alert('matches');
  }
})

Also if you just wanna check whether a key is present in the obejct use hasOwnProperty

此外,如果您只想检查obejct中是否存在键,请使用hasOwnProperty

testObj.hasOwnProperty('hey')

#5


1  

Object.keys(testObj) is return array not a string .see the console.log.hey is the first array first arguments.so you could match like this Object.keys(testObj) they will show the hey

Object.keys(testObj)是返回数组而不是字符串。看到console.log.hey是第一个数组的第一个参数。所以你可以像这样匹配Object.keys(testObj)他们会显示嘿

var testObj = {
  "hey": "blue"
};
console.log(Object.keys(testObj)); // alerts hey
if (Object.keys(testObj)[0] === "hey") {
console.log('matches');
}

Alert print the array as a string .see the both alert and console.log

警报将数组打印为字符串。同时显示alert和console.log

var array=['hi',' i am', 'array'];

console.log(array)
alert(array)

#1


2  

"Object.keys(testObj)" return an array. so basically you comparing an array. try below code snippet

“Object.keys(testObj)”返回一个数组。所以基本上你比较一个数组。尝试下面的代码片段

var testObj = {"hey":"blue"};
alert(Object.keys(testObj)); // alerts hey
console.log(Object.keys(testObj));
if (Object.keys(testObj)[0] === "hey") {
  alert('matches');
}

#2


1  

Object.keys() method returns an array of a given object's own enumerable properties.So you need to get the index from that array

Object.keys()方法返回给定对象自己的可枚举属性的数组。所以你需要从该数组中获取索引

var testObj = {
  "hey": "blue"
};
alert(Object.keys(testObj)); // alerts hey
//[0] will be first element in the array
if (Object.keys(testObj)[0] === "hey") {
  alert('matches');
}

#3


1  

You must need to specify index number in order to use it: check this.

您必须指定索引号才能使用它:检查一下。

<script>
function myfn()
{
    var testObj = {"hey":"blue"};
    alert(Object.keys(testObj)); // alerts hey
    if (Object.keys(testObj)[0] === "hey") {
      alert('matches');
    }
}
</script>


<body onload="myfn();"></body>

#4


1  

Object.keys(obj) retuns an array, so if you have multiple keys in the object then you can loop over them to match

Object.keys(obj)返回一个数组,所以如果你在对象中有多个键,那么你可以循环它们来匹配

var testObj = {"hey":"blue", "something": "else"};
console.log(Object.keys(testObj)); // alerts hey
Object.keys(testObj).forEach(function(key) {
   if (key === "hey") {
    alert('matches');
  }
})

Also if you just wanna check whether a key is present in the obejct use hasOwnProperty

此外,如果您只想检查obejct中是否存在键,请使用hasOwnProperty

testObj.hasOwnProperty('hey')

#5


1  

Object.keys(testObj) is return array not a string .see the console.log.hey is the first array first arguments.so you could match like this Object.keys(testObj) they will show the hey

Object.keys(testObj)是返回数组而不是字符串。看到console.log.hey是第一个数组的第一个参数。所以你可以像这样匹配Object.keys(testObj)他们会显示嘿

var testObj = {
  "hey": "blue"
};
console.log(Object.keys(testObj)); // alerts hey
if (Object.keys(testObj)[0] === "hey") {
console.log('matches');
}

Alert print the array as a string .see the both alert and console.log

警报将数组打印为字符串。同时显示alert和console.log

var array=['hi',' i am', 'array'];

console.log(array)
alert(array)