简体   繁体   English

在Javascript中对对象属性执行字符串方法

[英]Performing String Methods on Object Properties in Javascript

Whenever I try to perform string operations on variables that contain the values of object's properties in Javascript, I get an error about the interpreter not being able to find whatever the method Im trying to use for that object: 每当我尝试对包含Javascript中对象属性值的变量执行字符串操作时,我得到一个错误,关于解释器无法找到我试图用于该对象的方法:

var arrOfEntries = {};

arrOfEntries["name1"] = "Jane" ;
arrOfEntries["age1"] = "40" ;
arrOfEntries["name2"] = "Kathy" ;
arrOfEntries["age2"] = "35" ;
arrOfEntries["name3"] = "Mary" ;
arrOfEntries["age3"] = "23" ;
arrOfEntries["name4"] = "Donna" ;
arrOfEntries["age4"] = "32" ;

var result = ' ';
for (prop in arrOfEntries) {
    var tempVar = String(prop) /*/this line was added as an attempt to force the property into the type String, but does not work*
    if (tempVar.includes("name"))
        result += tempVar
    }

In this case, I simply wish to check for the existence of the string "name" within the name of each property of that object and then collect only these properties for further actions. 在这种情况下,我只想检查该对象的每个属性的名称中是否存在字符串“name”,然后仅收集这些属性以进行进一步的操作。 What am I missing here? 我在这里错过了什么?

prop is already a string. prop已经是一个字符串。 You should just need this: 你应该只需要这个:

for (prop in arrOfEntries) {
    if (arrOfEntries.hasOwnProperty(prop) && prop.indexOf("name") !== -1) {
        result += arrOfEntries[prop];
    }
}

I added hasOwnProperty so that you wouldn't erroneously try and add a method that already exists on the Object type. 我添加了hasOwnProperty这样您就不会错误地尝试添加Object类型上已存在的方法。

Also just FYI: arrOfEntries is an object (with string/value keys), not an array - there is some distinction in JavaScript types. 也就是FYI: arrOfEntries是一个对象(带有字符串/值键),而不是数组 - 在JavaScript类型中有一些区别。

Use largerString.indexOf("substring") , as includes() does not have wide browser support. 使用largerString.indexOf("substring") ,因为includes()没有广泛的浏览器支持。

The indexOf() method will tell you the starting position (as an integer) of the first match of substring within largerString . indexOf()方法将告诉您largerStringsubstring的第一个匹配的起始位置(作为整数)。 If no matches were found, it returns -1 如果未找到匹配项,则返回-1

See below: 见下文:

var arrOfEntries = {};

arrOfEntries["name1"] = "Jane" ;
arrOfEntries["age1"] = "40" ;
arrOfEntries["name2"] = "Kathy" ;
arrOfEntries["age2"] = "35" ;
arrOfEntries["name3"] = "Mary" ;
arrOfEntries["age3"] = "23" ;
arrOfEntries["name4"] = "Donna" ;
arrOfEntries["age4"] = "32" ;

var result = ' ';
for (prop in arrOfEntries) {
    if (prop.indexOf("name") >= 0) {
        result += prop
    }
}

If you do a console.log(result) you will get "name1name2name3name4" 如果你做一个console.log(result)你会得到"name1name2name3name4"

Do sth like that: 做那样的话:

var arrOfEntries = {};
arrOfEntries["name1"] = "Jane" ; arrOfEntries["age1"] = "40" ;
arrOfEntries["name2"] = "Kathy" ; arrOfEntries["age2"] = "35" ;
arrOfEntries["name3"] = "Mary" ; arrOfEntries["age3"] = "23" ;
arrOfEntries["name4"] = "Donna" ; arrOfEntries["age4"] = "32" ;
var result = ' ';
for (prop in arrOfEntries) {
if (prop.indexOf('name') != -1)
{
 alert(arrOfEntries[prop]);
}
}

Greetz 格尔茨

You have several issues: 你有几个问题:

  1. You're trying to force the value into a string by using var tempVar = String(prop) which isn't going to work because you left out the new operator. 您试图通过使用var tempVar = String(prop)将值强制为字符串,因为您省略了new运算符,所以它不起作用。 It should be var tempVar = new String(prop); 它应该是var tempVar = new String(prop); . You can ignore it though since it's not needed. 你可以忽略它,因为它不需要它。

  2. String.prototype.includes was introduced in ES6 and doesn't have wide browser support yet. String.prototype.includes是在ES6中引入的,目前还没有广泛的浏览器支持。 Check the documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes for more details. 有关详细信息, 查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes中的文档。

  3. Considering the lack of support for includes , you can use indexOf as a replacement: if (prop.indexOf('name') >= 0) { // 'name' is included } 考虑到缺少对includes的支持,您可以使用indexOf作为替换: if (prop.indexOf('name') >= 0) { // 'name' is included }

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

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