-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertObj.js
35 lines (24 loc) · 1.18 KB
/
convertObj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* -------------------------------------------------------
* Programming Question : Convert Object to Array and Vice Versa
* -------------------------------------------------------
**/
//? Convert Object to Array: WAF objectToArray that takes an obj as input and returns an array ot key-value pairs, where each element in the array is an array with two elements: the key and the corresponding value from object.
//? Convert Array to Object: WAF arrayToObject that takes an arr as input of key-value pairs(as returned by the objectToArray function) and returns a new object with the key and the corresponding value from array.
//? Ensure that the conversion functions work correctly for objects with various data types as values, such as strings, numbers and objects.
const obj = {
name: "Ganesh Bardade",
age: 29,
city: "Navi Mumbai"
};
function objectToArray(obj){
// let newArr = [];
// for(let key in obj){
// newArr.push(key,obj[key]);
// }
let entries = Object.entries(obj);
console.log("ObjectToArray", entries.flat());
let newEntries = Object.fromEntries(entries);
console.log("ArrayToObject", newEntries);
}
console.log(objectToArray(obj));