var obj = {
Fname1: "John",
Lname1: "Smith",
Age1: "23",
Fname2: "Jerry",
Lname2: "Smith",
Age2: "24"
}
带有类似此对象的对象。 请在密钥上使用 Regex 来获取数值, 如 Fname*, Lname *, 并获取值 。
var obj = {
Fname1: "John",
Lname1: "Smith",
Age1: "23",
Fname2: "Jerry",
Lname2: "Smith",
Age2: "24"
}
带有类似此对象的对象。 请在密钥上使用 Regex 来获取数值, 如 Fname*, Lname *, 并获取值 。
是的,你当然可以。
for(var key in obj) {
if(/^Fname/.test(key))
... do something with obj[key]
}
这是 Regex 方式, 但对于简单的东西, 您可能想要使用 < code> indexof () code > 。 如何使用? 这里如何 :
for(var key in obj) {
if(key.indexOf( Fname ) == 0) // or any other index.
... do something with obj[key]
}
如果您想要用属性列表来做某事, 我的意思是, 您想要所有属性的值, 您可以使用一个数组来存储这些属性, 使用regex/ indexof来匹配这些属性, 不管方便什么, 并且用这些值做一些事情... I d 将任务留给您 。
您可以使用更完整的对象来回避整个问题 :
var objarr = [
{fname: "John", lname: "Smith", age: "23"},
{fname: "jerry", lname: "smith", age: "24"}
] ;
objarr[0].fname; // = "John"
objarr[1].age; // = "24"
或者,如果你真的需要一个对象:
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"
现在,与其使用正正则,您可以很容易地通过改变数组指数来绕过数组:
for (var i=0; i<obj.people.length; i++) {
var fname = obj.people[i].fname;
// do something with fname
}
values = []
for(name in obj) {
if (obj.hasOwnProperty(name) && name.test(/Fname|Lname/i) values[name] = obj[name];
}
以 jquery :
$.each(obj, function (index,value) {
//DO SOMETHING HERE WITH VARIABLES INDEX AND VALUE
});
I love the new includes function. 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"
如果你还没有ES20152015汇编工作,您可以使用
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.