- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Retrieve key and values from object in an array JavaScript
We are required to write a JavaScript function that takes in an object that maps literal values. The function should create an array of array, each subarray should contain exactly two elements.
The first of which should be the key of corresponding object pair and second should be the value.
Example
const obj = { name: 'Nick', achievements: 158, points: 14730 }; const retrieveProperties = (obj = {}) => { const res = []; for(key in obj){ res.push([ key, obj[key] ]); }; return res; }; console.log(retrieveProperties(obj));
Output
And the output in the console will be −
[ [ 'name', 'Nick' ], [ 'achievements', 158 ], [ 'points', 14730 ] ]
- Related Articles
- How to return object from an array with highest key values along with name - JavaScript?
- Retrieve user id from array of object - JavaScript
- Sum from array values with similar key in JavaScript
- Creating a JavaScript Object from Single Array and Defining the Key Value?
- How to modify key values in an object with JavaScript and remove the underscore?
- Retrieve values from nested JSON array in MongoDB?
- Iterate through Object keys and manipulate the key values in JavaScript
- JavaScript: How to Create an Object from Key-Value Pairs
- Converting a JavaScript object to an array of values - JavaScript
- From JSON object to an array in JavaScript
- Select random values from an array in JavaScript?
- Extract unique values from an array - JavaScript
- Find unique and biggest string values from an array in JavaScript
- How to change an object Key without changing the original array in JavaScript?
- Looping numbers with object values and push output to an array - JavaScript?

Advertisements