使用正则表达式键匹配 Javascript 循环遍历对象获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10758112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:56:53  来源:igfitidea点击:

Loop through object get value using regex key matches Javascript

javascriptjqueryjsonobject

提问by vetri02

var obj = {
 Fname1: "John",
 Lname1: "Smith",
 Age1: "23",
 Fname2: "Jerry",
 Lname2: "Smith",
 Age2: "24"
}

with an object like this.Can i get the value using regex on key something like Fname*,Lname* and get the values.

使用这样的对象。我可以在键上使用正则表达式获取值,例如 Fname*,Lname* 并获取值。

回答by Parth Thakkar

Yes, sure you can. Here's how:

是的,当然可以。就是这样:

for(var key in obj) {
    if(/^Fname/.test(key))
        ... do something with obj[key]
}

This was the regex way, but for simple stuff, you may want to use indexOf(). How? Here's how:

这是正则表达式的方式,但对于简单的东西,您可能想要使用indexOf(). 如何?就是这样:

for(var key in obj) {
    if(key.indexOf('Fname') == 0) // or any other index.
        ... do something with obj[key]
}

And if you want to do something with a list of attributes, i mean that you want values of all the attributes, you may use an array to store those attributes, match them using regex/indexOf - whatever convenient - and do something with those values...I'd leave this task to you.

如果你想对属性列表做一些事情,我的意思是你想要所有属性的值,你可以使用一个数组来存储这些属性,使用 regex/indexOf 匹配它们 - 无论方便 - 并用这些值做一些事情......我会把这个任务留给你。

回答by Blazemonger

You can sidestep the entire problem by using a more complete object instead:

您可以通过使用更完整的对象来回避整个问题:

var objarr =  [
            {fname: "John",  lname: "Smith", age: "23"},
            {fname: "jerry", lname: "smith", age: "24"}
          ] ;

objarr[0].fname; // = "John"
objarr[1].age;   // = "24"

Or, if you really need an object:

或者,如果你真的需要一个对象:

var obj =  { people: [
            {fname: "John",  lname: "Smith", age: "23"},
            {fname: "jerry", lname: "smith", age: "24"}
          ]} ;

obj.people[0].fname; // = "John"
obj.people[1].age;   // = "24"

Now, instead of using a regex, you can easily loop through the array by varying the array index:

现在,您可以通过改变数组索引轻松地循环遍历数组,而不是使用正则表达式:

for (var i=0; i<obj.people.length; i++) {
    var fname = obj.people[i].fname;
    // do something with fname
}

回答by John Shepard

With jquery:

使用 jquery:

$.each(obj, function (index,value) {
    //DO SOMETHING HERE WITH VARIABLES INDEX AND VALUE
});

回答by agent-j

values = []
for(name in obj) {
   if (obj.hasOwnProperty(name) && name.test(/Fname|Lname/i) values[name] = obj[name];
}

回答by Ralph Cowling

Quick key matcher using includes()

使用 includes() 的快速键匹配器

I love the new includesfunction. You can use it to check for the existence of a key or return its value.

我喜欢新的包含功能。您可以使用它来检查键是否存在或返回其值。

var obj = {
   Fname1: "John",
   Lname1: "Smith",
   Age1: "23",
   Fname2: "Jerry",
   Lname2: "Smith",
   Age2: "24"
};
var keyMatch = function (object, str) {
for (var key in object) {
    if (key.includes(str)) {
        return key;
    }
}
return false;
};
var valMatch = function (object, str) {
for (var key in object) {
    if (key.includes(str)) {
        return object[key];
    }
}
return false;
};

// usage

console.log(keyMatch(obj, 'Fn')) // test exists returns key => "Fname1"
console.log(valMatch(obj, 'Fn')) // return value => "John"
    

If you haven't got ES2015 compilation going on, you can use

如果你还没有进行 ES2015 编译,你可以使用

~key.indexOf(str)

~key.indexOf(str)