[英]How to loop an object and remove all functions from it
讓我們解決這個問題,我需要從對象中刪除所有函數以通過socket.io JSON發送! 假設我有一個對象,例如...
let myObject = {
test1: "abc",
test2: function() {
console.log("This is a function");
},
test3: {
test4: "another string",
test5: function() {
console.log("another function");
},
test6: 123
}
}
我也要轉換
let myObject = {
test1: "abc",
test3: {
test4: "another string",
test6: 123
}
}
我嘗試了許多鱈魚方法,但都失敗了! 我會發布它們,但這將浪費您的寶貴時間。 我很樂意以一種精巧的功能解決此問題的任何人。 真的,雅各布·莫里斯(Jacob Morris)
PS這是做這件事的一個c ** p嘗試
let myObject = { test1: "abc", test2: function() { console.log("This is a function"); }, test3: { test4: "another string", test5: function() { console.log("another function"); }, test6: 123 } } let i = []; function tsfr(ls) { let i = {}; for (let a in ls) { if (typeof(ls[a]) !== "function") { let d = ls[a]; if (typeof(d) == "object") { d = tsfr(d); } i[a] = d; } } return i; } i = tsfr(myObject); console.log(i)
您可以編寫自己的遞歸解決方案,但我認為使用JSON.stringify更好。 默認情況下,stringify會忽略函數並將其從結果中刪除。 在某些情況下,第二個參數是替換函數,可以用於更高級的對象操作。
const removeFunctions = (obj) => { const stringified = JSON.stringify(obj); // We need to parse string back to object and return it const parsed = JSON.parse(stringified); return parsed; } const myObject = { test1: "abc", test2: function() { console.log("This is a function"); }, test3: { test4: "another string", test5: function() { console.log("another function"); }, test6: 123 } } console.log(removeFunctions(myObject));
(或在Codepen上 )
請注意,字符串化使用toString()
方法,對於某些自定義類實例,可能會導致數據丟失。 為避免這種情況,您需要編寫自己的遞歸解決方案。 它可能會變得更加復雜。
希望這會有所幫助,加油!
編輯:我剛剛看到了您的嘗試。 這是朝正確方向邁出的一步,但還需要更多的愛。 但是我需要建議您不要使用像tsfr
或ls
這樣的變量名。 如果使用更長,更具描述性的名稱,則代碼更具可讀性。
EDIT2:正如Andreas在評論中指出的那樣,您甚至不需要自定義替換器,因為stringify會忽略它們並默認將其刪除。
您可以像這樣使用delete
和遞歸地刪除嵌套對象屬性:
let myObject = { test1: "abc", test2: function() { console.log("This is a function"); }, test3: { test4: "another string", test5: function() { console.log("another function"); }, test6: 123 } } const deleteFunctions = (obj) => { Object.keys(obj).forEach(k => { if (typeof obj[k] === "function") delete obj[k]; else if (typeof obj[k] === "object") deleteFunctions(obj[k]) }) } deleteFunctions(myObject) console.log(myObject)
您可以通過類型檢查來篩選鍵/值對,然后通過檢查嵌套對象來映射新對象。
const removeFn = object => Object.assign(...Object .entries(object).filter(([k, v]) => typeof v !== 'function') .map(([k, v]) => ({ [k]: v && typeof v === 'object' ? removeFn(v) : v })) ); var object = { test1: "abc", test2: function() { console.log("This is a function"); }, test3: { test4: "another string", test5: function() { console.log("another function"); }, test6: 123 } }; console.log(removeFn(object));
只是為了好玩,我會這樣做。 這是對Douglas Crockford的DOM算法的重新審視。
首先,一個遞歸函數( visit()
)訪問一個任意對象並將一個任意函數應用於每個成員:
function visit(obj, func)
{
for (k in obj)
{
func(obj, k);
if (typeof obj[k] === "object")
{
visit(obj[k], func);
}
}
}
這是一個帶有有效負載功能的工作示例,該功能僅將找到的功能輸出到控制台:
var testdata = { "a": 1, "b": "hello", "c": [ 1, 2, 3 ], "d": (x) => 2 * x, "e": null, "f": { "x": 10, "y": { "z": "$$$", "zz": (x) => 0 } } }; function visit(obj, func) { for (k in obj) { func(obj, k); if (typeof obj[k] === "object") { visit(obj[k], func); } } } visit(testdata, (obj, k) => { if (typeof obj[k] === "function") { console.log(k + " is a function"); } });
如上面的代碼所示,有效載荷函數func
能夠找到對象中的所有函數。
現在我們需要的是一個刪除成員的函數,但這現在很容易:
(obj, k) => {
if (typeof obj[k] === "function")
{
delete obj[k];
}
}
通過將訪問與有效負載分開,您可以以多種方式重用此訪問,並為各種必需品遞歸操作對象...
它將刪除上述對象的所有功能
function removeFuncFromObj(obj) {
Object.keys(obj).map((key) => {
if (typeof obj[key] === "function") {
delete obj[key];
}
if (typeof obj[key] === typeof {}) {
removeFuncFromObj(obj[key]);
}
});
return obj;
}
removeFuncFromObj(myObject);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.