You can get the keys of each object inside an array of objects in many ways. In this article, we will cover two methods.
Methods to get the keys of each object
- Using Object.keys() and Object.assign()
- Using Array.flatMap() and Object.keys()
Method 1: Using Object.keys() and Object.assign()
Object.keys() – Object.keys() is a javascript method that returns an array of the enumerable property names of the given object. In other words, it extracts the keys of an object and returns them as an array. The order of the keys in the array is determined by the order in which they are defined in the object.
Object.assign() – Object.assign() is a method in JavaScript used to copy the values of all enumerable properties from one or more source objects to a target object. It returns the modified target object.
let obj = [{"a": 1}, {"b" : 2}, {"c" : 3}];
let result = Object.keys(Object.assign(
{},
...obj
));
console.log(result);
The Output of the above code will be
(3) ["a", "b", "c"]
0:"a"
1:"b"
2:"c"
[[Prototype]]:[]
Method 2: Using Array.flatMap() and Object.keys()
Array.flatMap() – Array.flatMap() is a method in JavaScript that both maps and flattens an array. It applies a provided mapping function to each array element and then flattens the result into a new array.
let obj = [{"a": 1}, {"b" : 2}, {"c" : 3}];
let result = obj.flatMap(Object.keys);
console.log(result);
The Output of the above code will be
(3) ["a", "b", "c"]
0:"a"
1:"b"
2:"c"
[[Prototype]]:[]
Similar Reads
- Live Text Highlighter Using HTML, CSS, and JavaScriptIn this post, we’re going to build search feature that highlight words in real-time as you type — from scratch — using just HTML, CSS,… Read more: Live Text Highlighter Using HTML, CSS, and JavaScript
- Top 10 AI Tools Revolutionizing Workflows in 2025AI isn’t a trend anymore. It’s infrastructure. In 2025, the smartest teams aren’t just using AI—they’re building it into everything. From automating repetitive tasks to… Read more: Top 10 AI Tools Revolutionizing Workflows in 2025
- 15+ CSS Navigation Menus – CodepenLooking to create visually appealing and user-friendly navigation for your website? Explore our collection of free HTML and CSS navigation menu code examples that showcase… Read more: 15+ CSS Navigation Menus – Codepen
- 14+ CSS Grid Layouts Examples – CodepenLooking to create visually appealing and structured designs for your website? Explore our collection of free HTML and CSS grid layout code examples that showcase… Read more: 14+ CSS Grid Layouts Examples – Codepen
- What Are Micro Frontends and How to Implement Them with ReactIn modern web development, building scalable and maintainable applications is crucial. One approach that has gained popularity is Micro Frontends. If you’re curious about what Micro… Read more: What Are Micro Frontends and How to Implement Them with React