简体   繁体   English

如何在Javascript中使用'in'关键字

[英]How to use 'in' keyword in Javascript

I have a question here. 我在这里有一个问题。 I know my code have plenty of problems and I need your help. 我知道我的代码有很多问题,需要您的帮助。 The problem is, To return an object which have numbers of repetition information of the string. 问题是,要返回具有字符串重复信息数目的对象。 The given string is 给定的字符串是

var r = countFreq(["a", "b", "b", "c", "c", "c", "d"]);

and the result have to be 结果必须是

{"a":1, "b":2, "c":3, "d":1 }

by console.log(r); 通过console.log(r);

All that I know is, key(properties) have to be element and value(value of property) have to be the number of repetition. 我所知道的是,键(属性)必须是元素,值(属性的值)必须是重复的次数。 AND, I must use 'in' key world to solve this problem. 而且,我必须使用“关键”世界来解决此问题。

Like, 
if('property' in 'object')  {
 //...
}else {
 //...
}

(if there's no property initialize as 1, and if there's a same property, add 1 each time) (如果没有属性初始化为1,并且有相同属性,则每次加1)

I really appreciate your help. 非常感谢您的帮助。 (This post may have grammatical errors. I really feel sorry about that...) (此帖子可能有语法错误。对此我感到非常抱歉...)

      function countFreq(array) {
        var i;
        for(i=0; i<array.length; i++)
        {
          if(array[i] in array)
          {
            return i += 1;
          }else
          {
            return i = 1;
          }
          console.log(array[i]+": "+i+", ");
        }
      }
      var r = countFreq(["a","b","c","c","c","d"]);
      console.log(r);

According to MDN - The 'in' operator returns true if the specified property is in the specified object or its prototype chain. 根据MDN-如果指定的属性位于指定的对象或其原​​型链中,则“ in”运算符将返回true。 Prop is a string or symbol representing a property name or array index (non-symbols will be coerced to strings). Prop是代表属性名称或数组索引的字符串或符号(非符号将被强制转换为字符串)。 Object is to check if it (or its prototype chain) contains the property with specified name. 对象将检查其(或其原型链)是否包含具有指定名称的属性。

So in your case, it depends what your object is? 因此,就您而言,这取决于您的对象是什么? if you object is an array, you need to use prop as properties of array. 如果对象是数组,则需要使用prop作为数组的属性。 All index values up to length of array will return true. 直到数组长度的所有索引值都将返回true。 MDN Example of arrays MDN数组示例

var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
0 in trees        // returns true
3 in trees        // returns true
6 in trees        // returns false
'bay' in trees    // returns false (you must specify the 
                  // index number, not the value at that index)
'length' in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES2015+)

I think you are misunderstanding the in operator. 我认为您误会了in运算符。 In can be used in 2 cases, as a boolean operator to check for the presence of an index in an array or to iterate over the indexes of an array with a for loop. in可以在2种情况下用作布尔运算符,以检查数组中是否存在索引或使用for循环遍历数组的索引。 You are using it to check for the presence of a value in an array directly, which you cannot do. 您正在使用它直接检查数组中是否存在值,而您不能这样做。 Also you are returning from the function after each iteration so you will only ever get 1 or 0. 另外,每次迭代后您都将从函数中返回,因此只会得到1或0。

I presume you want something like the following: 我假设您想要以下内容:

countFreq(array) {
    var results = { a: 0, b: 0, c: 0, d: 0 };
    for (var index in array) {
        results[array[index]] ++;
    }
    return results;
}

Now you can access each result with results['a'] for instance, after calling countFreq . 现在,在调用countFreq之后,可以使用例如result results['a']访问每个结果。 I think you need to read up on return and loops in JavaScript. 我认为您需要阅读JavaScript中的return和loops。

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

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