Several issues:

  • forEach always returns undefined. You need to use map instead.
  • (key, value["Penetration_2018"]) is not a tuple, but a comma operator expression and results in the value of value["Penetration_2018"]. Use square bracket notation instead.
  • A Map has no useful representation when cast to string. You need to turn it to an array or something else to get a more useful string representation:

Code:

const mapTest = new Map(
    Object.entries(filterCounties).map(
        ([key, value]) => [key, value["Penetration_2018"]]
    )
);
console.log(`This is mapTest ${JSON.stringify([...mapTest])}`);

With arrays it is more conventional to just apply map directly on the array, and it is a mystery why you would need a Map anyway. If you want the unique values, then use a Set:

const uniques = [...new Set(filterCounties.map(value => value["Penetration_2018"]))];
console.log(`This is uniques: ${uniques}`);
Answer from trincot on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript | MDN
Object.entries() returns an array whose elements are arrays corresponding to the enumerable string-keyed property key-value pairs found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.
🌐
W3Schools
w3schools.com › jsref › jsref_object_entries.asp
W3Schools.com
Object.entries() returns the keys and values of any object types.
Discussions

Object.entries with forEach to map array of objects returns ...
0 Mapping Array of Objects Returns an Undefined Instead of Actual Objects · 1 JS : console.log displays "undefined" in the end of an array of objects More on stackoverflow.com
🌐 stackoverflow.com
node.js - Is it a bug on javascript Object entries foreach? - Stack Overflow
I'm trying to convert an object to list of key-value pairs using Object.entries and then i loop each of the entry, everything is fine except the order of result is different with the object i give ... More on stackoverflow.com
🌐 stackoverflow.com
I m confused with Object.entries()[i]
JSON is Javascript Object Notation, a way of formatting a string to represent an object. A string can be in JSON format, but an (unserialized) object is just an object. More on stackoverflow.com
🌐 stackoverflow.com
For...in vs Object.keys
It's largely preference or depending on whether or not you're dealing with a more imperative or more functional codebase. But you should at least be aware of the differences that go beyond style. Specifically, for..in will capture inherited keys whereas Object.keys sticks only to own properties. For example: var par = { prop1 : "some val" }; var obj = Object.create(par); obj.prop2 = "some other val"; for(key in obj){ console.log("Key: ", key) console.log("Value: ", obj[key]) } // ^ prop1 and prop2 Object.keys(obj).forEach((key)=>{ console.log("For Each Key: ", key) console.log("For Each Value: ", obj[key]) }) // ^ prop2 only More on reddit.com
🌐 r/javascript
24
14
April 24, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-entries-method
JavaScript Object entries() Method - GeeksforGeeks
Object.entries() returns an array consisting of enumerable property [key, value] pairs of the object passed.
Published   September 18, 2024
Top answer
1 of 3
9

Several issues:

  • forEach always returns undefined. You need to use map instead.
  • (key, value["Penetration_2018"]) is not a tuple, but a comma operator expression and results in the value of value["Penetration_2018"]. Use square bracket notation instead.
  • A Map has no useful representation when cast to string. You need to turn it to an array or something else to get a more useful string representation:

Code:

const mapTest = new Map(
    Object.entries(filterCounties).map(
        ([key, value]) => [key, value["Penetration_2018"]]
    )
);
console.log(`This is mapTest ${JSON.stringify([...mapTest])}`);

With arrays it is more conventional to just apply map directly on the array, and it is a mystery why you would need a Map anyway. If you want the unique values, then use a Set:

const uniques = [...new Set(filterCounties.map(value => value["Penetration_2018"]))];
console.log(`This is uniques: ${uniques}`);
2 of 3
3

Object.entries is pretty useless on an array. If necessary, you'd use the array entries method.

I thought that when using an arrow function, return was implicit?

An arrow function with a concise body implicitly returns the expression value, yes. However, in your first example, console.log does not return anything, and in both examples, forEach returns nothing. And in your second example, you use the comma operator for some kind of "tuple" notation while the Map constructor requires arrays.

Instead of trying to use "forEach to map array of objects", you should use the map method to map an array of objects!

const mapTest = new Map(filterCounties.map((value, key) => [key, value.Penetration_2018]));
console.log('This is mapTest:', mapTest);
Find elsewhere
Top answer
1 of 2
2

The Docs say that Object.entries returns an array of given objects enumerable property [key,value] pairs . So yes its confusing if you look at this statement

const object3 = { 100: 'a', 2: 'b', 7: 'c' };

and end up getting ["2", "b"] when you call Object.entries(object3)[0].

When you are doing this Object.entries(object3)[0] , you are accessing a pair at the index of 0 returned by this function Object.entries(object) . The order of this array has nothing to do with how you defined the object3 in the first place. The order according to the doc is the same as the provided by a for...in loop. I ran the for...in loop on the object and this is what i got as the order.

2,7,100.

This is why you are getting ["2", "b"] instead of ["100", "a"]. As others have mentioned here , the order seems to be that way because 2<7<100.

2 of 2
0

This is because the object has numeric keys and when you manipulate the object with the numeric keys the javascript sort the key-values in ascending order of the key value and since you have keys 2, 7, and 100. So, check this when you console.log the object Object.entries(object3) you get that sorted and when you access [0] you get Array ["2", "b"]

const object3 = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(object3));

Further clarification on sorting the object by ascending values of key when they are numeric. The javascript sorts them behind the scenes.

var a = {
  10: 'ten',
  5: 'five',
  11: 'eleven',
  1: 'one'
};
console.log(a);

🌐
Reddit
reddit.com › r/javascript › for...in vs object.keys
r/javascript on Reddit: For...in vs Object.keys
April 24, 2018 -

Hey all, this is solely for for my own curiosity, but what would be preferred in any given situation while iterating over an object, "for...in", or "Object.keys(obj).forEach()"?

I generally favor "for...in", but it occurred to me the .forEach() method may work just as well.

Example:

var obj = { prop1 : "some val", prop2 : "some other val"};

for(key in obj){

console.log("Key: ", key)

console.log("Value: ", obj[key])

}

Object.keys(obj).forEach((key)=>{

console.log("For Each Key: ", key)

console.log("For Each Value: ", obj[key)

})

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Sorting a converted object (Object.entries)
May 26, 2019 - I have a large object of key:value pairs to sort by the value. I am trying to convert to an array using Object.entries, I believe it results in an array of arrays (rather than an array of objects) so I am trying access the value by using index 1, rather than dot notation: sortBy = () => { const countries = this.props.countriesToRender; const countriesArray = Object.entries(countries); return countriesArray.sort((a, b) => { return b[1] - a[1]; }); }; so far it doesn’t...
Top answer
1 of 16
5192

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

For-of with Object.keys() alternative:

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Notice the use of for-of instead of for-in, if not used it will return undefined on named properties, and Object.keys() ensures the use of only the object's own properties without the whole prototype-chain properties

Using the new Object.entries() method:

Note: This method is not supported natively by Internet Explorer. You may consider using a Polyfill for older browsers.

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (const [key, value] of Object.entries(p)) {
  console.log(`${key}: ${value}`);
}
2 of 16
1383

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

You can combine for...of, destructuring, and Object.entries:

for (const [key, value] of Object.entries(obj)) {
    console.log(key, value);
}

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › fromEntries
Object.fromEntries() - JavaScript | MDN
October 30, 2025 - The Object.fromEntries() static method transforms a list of key-value pairs into an object. const entries = new Map([ ["foo", "bar"], ["baz", 42], ]); const obj = Object.fromEntries(entries); console.log(obj); // Expected output: Object { foo: "bar", baz: 42 }
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Object › entries
JavaScript Object entries() - Get Object Entries | Vultr Docs
September 27, 2024 - The Object.entries() method in JavaScript is a useful tool for accessing a list of a given object's own enumerable string-keyed property [key, value] pairs in the form of an array.
🌐
Programiz
programiz.com › javascript › library › object › entries
JavaScript Object.entries()
The Object.entries() method returns an array of key-value pairs of an object's enumerable properties.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-object-entries-values
Object.values and Object.entries in JavaScript | DigitalOcean
May 26, 2022 - Similar to the Objects.values() method, the Object.entries() method returns a nested array with key-value pairs.
🌐
Codecademy
codecademy.com › docs › javascript › objects › .entries()
JavaScript | Objects | .entries() | Codecademy
December 11, 2023 - The .entries() method returns an array containing arrays of an object‘s key-value pairs in the following format: [ [key, value], [key, value], ...
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects in JavaScript? - Flexiple
Loop through objects' keys and ... properties and their corresponding values. The Object.keys() method returns an array of a given object's own enumerable property names....
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript object methods › javascript object.entries()
JavaScript Object.entries() Method
May 8, 2024 - ES2017 introduces the Object.entries() method that accepts an object and returns its own enumerable string-keyed property [key, value] pairs of the object.
🌐
Quora
quora.com › How-can-you-use-the-Object-keys-Object-values-and-Object-entries-methods-to-access-the-properties-and-values-of-an-object-in-JavaScript
How to use the Object.keys, Object.values, and Object.entries methods to access the properties and values of an object in JavaScript - Quora
Answer: [code]const myObject = { a: 1, b: 2, c: 3 }; // Using Object.keys to get property names const keys = Object.keys(myObject); console.log(keys); // Output: ['a', 'b', 'c'] // Using Object.values to get property values const values = Object.values(myObject); console.log(values); // Outpu...
🌐
DEV Community
dev.to › attacomsian › object-entries-and-object-values-methods-in-javascript-3l8c
Object.entries Javascript: Object.entries() and Object.values() Methods in JavaScript - DEV Community
February 17, 2020 - const entries = new Map([ ['foo', 'bar'], ['baz', 42] ]); const obj = Object.fromEntries(entries); console.log(obj); // expected output: Object { foo: "bar", baz: 42 } ... I write about modern JavaScript, Node.js, Spring Boot, and all things web development.