我有一個res包含一些空值的陣列,并且我有一個函式remove應該回傳一個洗掉了空值和未定義的陣列,但我無法讓它在我的陣列上作業。我已經看到了很多關于這類事情的答案,事實上我的remove功能起源于其中一個,但我似乎無法讓它發揮作用。
res =
[
{
"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": [
"[email protected]",
"[email protected]",
null,
"[email protected]"
]
},
{
"149Lmt-gweagewfrthjregjiojoinONEDOnonao": [
"[email protected]"
]
},
{
"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": [
"[email protected]",
"[email protected]"
]
},
{
"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [
null,
"[email protected]"
]
},
{
"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": [
"[email protected]"
]
},
{
"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": [
"[email protected]"
]
},
{
"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": [
"[email protected]"
]
}
]
var remove = function (array) {
var result = [];
array.forEach(function (item) {
if (Array.isArray(item) && item.length!=0) {
// Item is a nested array, go one level deeper recursively
result.push(remove(item));
}
else if (typeof item !== null) {
result.push(item);
}
});
return result;
};
console.log(remove(res));
uj5u.com熱心網友回復:
這是一個沒有遞回的解決方案,適用于示例中給出的嵌套級別。如果單個陣列元素具有多個鍵值對,也將起作用。
let res =[{"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": ["[email protected]","[email protected]",null,"[email protected]"]},{"149Lmt-gweagewfrthjregjiojoinONEDOnonao": ["[email protected]"]},{"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": ["[email protected]","[email protected]"]},{"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [undefined,"[email protected]"]},{"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": ["[email protected]"]},{"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": ["[email protected]"]},{"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": ["[email protected]",null]}]
var remove = function (array) {
return array.map(function (item) {
let x = Object.entries(item).map((y) => {
return [y[0],y[1].filter((z)=> z!==undefined && z!==null)]
})
return Object.fromEntries(x)
});
};
console.log(remove(res));
uj5u.com熱心網友回復:
如果你想洗掉nulls 以及undefineds,你可能想替換else if (typeof item !== null)為else if (typeof item != null)
uj5u.com熱心網友回復:
發生了什么
的元素res是物件。請注意,在嵌套函式呼叫中remove
result.push(remove(item));
被傳遞的是因此不是陣列item的元素。res所以當remove(item)被呼叫時,檢查Array.isArray(item)失敗并且沒有任何東西被整理出來。要獲取內部陣列,請添加此行。
var values = Object.values(item)
現在處理 item 為null和Object的情況Array。
解決方案
這是我對解決方案的嘗試。(我希望你不介意 ES6)這確實適用于這個特定的(不確定其他情況)
const remove = (item) => {
if (item) {
console.log('not null', item)
if (Array.isArray(item)) {
const result = []
for (let elem of item) {
const cleanedElem = remove(elem)
// Maybe use Nullish coalescing operator ?
if (cleanedElem !== null && cleanedElem !== undefined)
result.push(cleanedElem)
}
return result
} else if (typeof item === 'string' || typeof item === 'number') {
return item
} else if (item) {
const result = {}
for (let pair of Object.entries(item)) {
const [key, value] = pair
const cleanedValue = remove(value)
// Maybe use Nullish coalescing operator ?
if (cleanedValue !== null && cleanedValue !== undefined)
result[key] = remove(cleanedValue)
}
return result
}
}
}
cleansed = remove(res)
console.log(cleansed);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438172.html
標籤:javascript 节点.js 数组 json 筛选
