-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameKeyObject.js
More file actions
68 lines (61 loc) · 1.69 KB
/
RenameKeyObject.js
File metadata and controls
68 lines (61 loc) · 1.69 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Change the keys of an object with a list on an array
* @param {Object} obj
* @param {Array} newKeys
* @returns {Object}
*/
const renameKey = (obj, newKeys) => {
const oldKey = Object.keys(obj);
let newObj = {};
for (let index = 0; index < newKeys.length; index++) {
newObj = { ...newObj, [newKeys[index]]: obj[oldKey[index]] };
}
return newObj;
};
const objTochange = {
test1: { subTest1: "" },
test2: { subTest2: "" },
test3: { subTest3: "jhg" },
};
const keys = ["test5", "test6", "test7"];
console.log(renameKey(objTochange, keys));
/* return {
test5: { subTest1: '' },
test6: { subTest2: '' },
test7: { subTest3: 'jhg' }
} */
/**
* Rename Object keys with Array of Objects
* @param {Object} obj to rename keys
* @param {Array} columns content Object with key (title and key)
* @returns {Object} return obj modified
*/
const renameKeyWithArrOfObj = (obj, columns) => {
const allOldKey = Object.keys(obj);
let oldKey = "";
for (let i = 0; i < allOldKey.length; i+=1) {
const newKey = columns.filter((el) => {
if (el.key === allOldKey[i]) {
oldKey = allOldKey[i];
return true;
}
return false;
})[0].title;
if (oldKey !== newKey) {
Object.defineProperty(
obj,
newKey, // modify old key
// fetch description from object
Object.getOwnPropertyDescriptor(obj, oldKey)
);
delete obj[oldKey]; // delete old key
}
}
return obj;
};
const obj = { test: "pouf", essai: "kjhlkh" };
const columns = [
{ title: "Test", key: "test" },
{ title: "Essai", key: "essai" }
];
console.log(renameKeyWithArrOfObj(obj, columns)); // return {Test: "pouf", Essai: "kjhlkh"}