jquery - Get Javascript Object Property Name - Stack Overflow

I'd like to know if are there any way to get field name or property name dynamically inside my jqu

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

You 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745294412a4621051.html

相关推荐

  • jquery - Get Javascript Object Property Name - Stack Overflow

    I'd like to know if are there any way to get field name or property name dynamically inside my jqu

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信