admin管理员组文章数量:1279051
I'm just trying to make a little framework for learning purposes, and I want to be able to use Array.prototype.forEach
on objects, this is what I have right now,
var data = { test: 'test' };
Array.prototype.forEach(data, function(key value) {
console.log(key);
});
But I get an error, I'm sure you guys can see why, but I can not :) So any help would be great thanks :)
I'm just trying to make a little framework for learning purposes, and I want to be able to use Array.prototype.forEach
on objects, this is what I have right now,
var data = { test: 'test' };
Array.prototype.forEach(data, function(key value) {
console.log(key);
});
But I get an error, I'm sure you guys can see why, but I can not :) So any help would be great thanks :)
Share Improve this question edited May 13, 2013 at 16:00 Ben McCormick 25.7k12 gold badges55 silver badges71 bronze badges asked May 13, 2013 at 15:57 DatsikDatsik 14.8k15 gold badges85 silver badges128 bronze badges 1- I'd assume the "Why?" is because an object isn't an array. You also have a syntax error in the parameters in your anonymous function. – Anthony Grist Commented May 13, 2013 at 15:59
6 Answers
Reset to default 5Objects are not arrays, and don't have access to the array prototype. You can just loop over the properties.
for(var key in data){
console.log(key) //or data[key] if you want the values
}
with regards to the jQuery ment below, it appears they use loops internally for their "each" function. From the source:
// args is for internal usage only
each: function( obj, callback, args ) {
var value,
i = 0,
length = obj.length,
isArray = isArraylike( obj );
if ( args ) {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
} else {
for ( i in obj ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
}
// A special, fast, case for the most mon use of each
} else {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break;
}
}
} else {
for ( i in obj ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break;
}
}
}
}
return obj;
},
As mentioned above, Object and Array are different things. You can create a function that will do this for you, by using "call" for invocation:
Object.prototype.forEach = function(func, context) {
var value;
context = context || this; //apply the function to 'this' by default
for (key in this) {
if (this.hasOwnProperty(key)) { //to rule out inherited properties
value = this[key];
func.call(context, key, value);
}
}
};
And use it like this:
obj = { "test1": 1, "test2": 2 };
obj.forEach(function(key, val) {
console.log(key, val);
});
Do it like this:
Object.keys(data).forEach(function(key) {
console.log(data[key])
});
You can shim Object.keys()
for old browsers if needed.
If you prefer, you can make a wrapper function for this operation:
function objEach(obj, fn) {
Object.keys(obj).forEach(function(key) {
fn(obj[key], key)
})
// or use for-in
}
Then use it like this:
objEach(data, function(val, key) {
console.log(key, val)
})
With ES2017, there is Object.entries()
:
const obj = { 0:'value1', key:'value2' };
Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});
For ES5, there is Object.keys()
:
const obj = { 0:'value1', key:"value2" };
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
If your object is Array-like (meaning that it has numerical keys and a length
), there is ES2015's Array.from()
:
const obj = { 0:'value1', 1:'value2', length:2 };
Array.from(obj).forEach((element, i) => {
console.log(i, element);
});
You can iterate through the objects keys by for(key in data)
and get each value by data[key]
. I.e.
for(key in object) {
console.log(key, data[key]);
}
Try this:
obj = {}; // Some object
obj.forEach = function(fn) {
Object.keys(this).forEach((k)=> {
if (typeof(this[k]) != 'function')
fn(this[k]);})
};
本文标签: javascriptHow might I be able to use ArrayprototypeforEach on an objectStack Overflow
版权声明:本文标题:javascript - How might I be able to use Array.prototype.forEach on an object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741210360a2358990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论