从多维数组中删除 Nullish 值?
Remove Nullish values from multidimensional array?
我有一个带有一些空值的数组 res
,我有一个函数 remove
应该 return 一个删除了空值和未定义的数组,但我不能让它在我的阵列上工作。我已经看到很多关于这类事情的答案,事实上我的 remove
函数源自其中一个,但我似乎无法让它工作。
res =
[
{
"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": [
"raggy@champ.co",
"grope@champ.co",
null,
"halp@champ.co"
]
},
{
"149Lmt-gweagewfrthjregjiojoinONEDOnonao": [
"raggy@champ.co"
]
},
{
"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": [
"wlyman@champ.co",
"raggy@champ.co"
]
},
{
"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [
null,
"raggy@champ.co"
]
},
{
"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": [
"raggy@champ.co"
]
},
{
"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": [
"raggy@champ.co"
]
},
{
"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": [
"raggy@champ.co"
]
}
]
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));
如果您想删除 null
和 undefined
,您可能需要将 else if (typeof item !== null)
替换为 else if (typeof item != null)
这是一个没有递归的解决方案,适用于示例中给出的嵌套级别。如果单个数组元素具有多个键值对,也将起作用。
let res =[{"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": ["raggy@champ.co","grope@champ.co",null,"halp@champ.co"]},{"149Lmt-gweagewfrthjregjiojoinONEDOnonao": ["raggy@champ.co"]},{"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": ["wlyman@champ.co","raggy@champ.co"]},{"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [undefined,"raggy@champ.co"]},{"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": ["raggy@champ.co"]},{"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": ["raggy@champ.co"]},{"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": ["raggy@champ.co",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));
发生了什么事
res
的元素是对象。
请注意,在 remove
的嵌套函数调用中
result.push(remove(item));
传递的 item
是 res
的元素,因此不是数组。因此,当 remove(item)
被调用时,检查 Array.isArray(item)
失败并且没有任何结果。
要获取内部数组,请添加此行。
var values = Object.values(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);
我有一个带有一些空值的数组 res
,我有一个函数 remove
应该 return 一个删除了空值和未定义的数组,但我不能让它在我的阵列上工作。我已经看到很多关于这类事情的答案,事实上我的 remove
函数源自其中一个,但我似乎无法让它工作。
res =
[
{
"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": [
"raggy@champ.co",
"grope@champ.co",
null,
"halp@champ.co"
]
},
{
"149Lmt-gweagewfrthjregjiojoinONEDOnonao": [
"raggy@champ.co"
]
},
{
"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": [
"wlyman@champ.co",
"raggy@champ.co"
]
},
{
"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [
null,
"raggy@champ.co"
]
},
{
"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": [
"raggy@champ.co"
]
},
{
"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": [
"raggy@champ.co"
]
},
{
"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": [
"raggy@champ.co"
]
}
]
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));
如果您想删除 null
和 undefined
,您可能需要将 else if (typeof item !== null)
替换为 else if (typeof item != null)
这是一个没有递归的解决方案,适用于示例中给出的嵌套级别。如果单个数组元素具有多个键值对,也将起作用。
let res =[{"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": ["raggy@champ.co","grope@champ.co",null,"halp@champ.co"]},{"149Lmt-gweagewfrthjregjiojoinONEDOnonao": ["raggy@champ.co"]},{"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": ["wlyman@champ.co","raggy@champ.co"]},{"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [undefined,"raggy@champ.co"]},{"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": ["raggy@champ.co"]},{"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": ["raggy@champ.co"]},{"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": ["raggy@champ.co",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));
发生了什么事
res
的元素是对象。
请注意,在 remove
result.push(remove(item));
传递的 item
是 res
的元素,因此不是数组。因此,当 remove(item)
被调用时,检查 Array.isArray(item)
失败并且没有任何结果。
要获取内部数组,请添加此行。
var values = Object.values(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);