admin管理员组文章数量:1418669
I'd like to know if are there any way to get field name or property name dynamically inside my jquery.grep function
<script type="text/javascript">
var names = [];
var object = {
name: "Joe",
age: 20,
email: "[email protected]"
};
names.push(object);
object = {
name: "Mike",
age: 50,
email: "[email protected]"
};
names.push(object);
object = {
name: "Joe",
age: 45,
email: "[email protected]"
};
names.push(object);
var found_names = $.grep(names, function(v,y) {
return v.name === "Mike" && v.age <= 50;
});
$(document).ready(function(){
console.log(found_names);
});
</script>
what i get actually with this code are name and age values according a condition but what about if i wanted to get the key names i.e. name, age, email. Is that possible on my function?
I'd like to know if are there any way to get field name or property name dynamically inside my jquery.grep function
<script type="text/javascript">
var names = [];
var object = {
name: "Joe",
age: 20,
email: "[email protected]"
};
names.push(object);
object = {
name: "Mike",
age: 50,
email: "[email protected]"
};
names.push(object);
object = {
name: "Joe",
age: 45,
email: "[email protected]"
};
names.push(object);
var found_names = $.grep(names, function(v,y) {
return v.name === "Mike" && v.age <= 50;
});
$(document).ready(function(){
console.log(found_names);
});
</script>
what i get actually with this code are name and age values according a condition but what about if i wanted to get the key names i.e. name, age, email. Is that possible on my function?
Share Improve this question asked Oct 27, 2015 at 23:42 Cabezz CabezzCabezz Cabezz 271 silver badge8 bronze badges2 Answers
Reset to default 4You can loop through the properties of an object by doing this-
var myObj = { name: "John", age: 28 }
for (var key in myObj) {
console.log("key: " + key);
console.log("value: " + myObj[key]);
}
//prints
key: name
value: John
key: age
value: 28
When you used grep you basically created a new array with the objects that has a name "Mike" and are below 50 or equal to 50. In your case you can loop through your new array and print the key and value.
found_names.forEach(function (obj) {
console.log('Name: ' + obj.name);
console.log('Age: ' + obj.age);
console.log('Email: ' + obj.email);
});
If you want the index(key) you can also do
found_names.forEach(function (obj, index) {
console.log('Index: ' + index);
console.log('Name: ' + obj.name);
console.log('Age: ' + obj.age);
console.log('Email: ' + obj.email);
});
Edit 2 -
If you have a dynamic object where you don't know the keys you can use what Thiago suggested and tweaking it a bit. The example below would loop through every object then would go through its data.
found_names.forEach(function (obj) {
for(key in obj){
console.log(key + ': ' + obj[key]);
}
});
Another example would be using an array like object where you loop through an object like you are looping in an array.
found_names.forEach(function (obj, index, array) {
array.forEach(function(data){
// data would be the current index
console.log(array[data]);
});
});
Here is an example on JSFiddle
Source for forEach Docs
本文标签: jqueryGet Javascript Object Property NameStack Overflow
版权声明:本文标题:jquery - Get Javascript Object Property Name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745294412a2651987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论