Retrieve key and values from object in an array JavaScript


In the given problem we are required to retrieve the key and values from an object in an array with the help of Javascript. So here we will retrieve the keys and values from an object in an array with the help of two methods provided by the object class in Javascript.

Understanding the Problem Statement

The problem statement is to retrieve the keys and values from a given object in an array with the help of Javascript. So in Javascript the object is a collection of properties in which every property is a key and value pair. To retrieve the keys and values from an object in an array we will use two methods of Javascript by object class.

Logic for the above Problem

To get the keys and its corresponding values from an object we can use two methods given by the object class in Javascript. Object.keys(obj) and Object.keys(obj) are the methods to return an array of keys and array of values. To get the keys and values from every object in an array we can use a loop to traverse through the array and call the Object.keys and Object.values methods for every object.

In the code we will initialize an array of objects and get the keys from the first object in the array with the help of Object.keys and then we will loop through every object in the array. For every object we will get the values with the help of Object.values and get the output as the object, keys and values for every iteration.

Algorithm

Step 1: As we have to retrieve key and value from an object in an array so first we will define an array of objects. For retrieving the keys and values from.

Step 2: After defining the object next we will use the Object.keys method to get the keys.

Step 3: Now we will have to use a loop to iterate through every object in the array.

Step 4: Inside the above loop, we will get the values for every object with the help of the Object.values method.

Step 5: At the end we will print the object, keys and values for every iteration in the function.

Example

const array = [
  {name: 'Amit', age: 30},
  {name: 'Ajay', age: 25},
  {name: 'Ankit', age: 35}
];

// get the keys from first object in array
const keys = Object.keys(array[0]);

for (const obj of array) {
  // get values for current object
  const values = Object.values(obj);
  console.log(`Object: ${JSON.stringify(obj)}, Keys: ${keys}, Values: 
${values}`);
}

Output

Object: {"name":"Amit","age":30}, Keys: name,age, Values: Amit,30
Object: {"name":"Ajay","age":25}, Keys: name,age, Values: Ajay,25
Object: {"name":"Ankit","age":35}, Keys: name,age, Values: Ankit,35

Complexity

The time complexity for getting the keys, values and objects in the function is O(n^2). Here n is the number of objects in the array. As we have nested a loop in which we will loop through every object in the array and then loop through every key in the object. The space complexity is O(n) because we will create a new array of values for every object in the array.

Conclusion

In the above mentioned algorithm we have shown how to get the keys and values from an object in an array with the help of Javascript. We have used the Object.keys and Object.values method to get the keys and values respectively. And the time and space complexity of this algorithm is O(n^2) and O(n) respectively.

Updated on: 16-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements