gpt4 book ai didi

javascript - 从对象中删除未定义的值

转载 作者:行者123 更新时间:2023-12-02 09:03:06 25 4
gpt4 key购买 nike

我在 javascript 中遇到问题,我想从对象中删除未定义的值

预期输出 数组中没有未定义值。

var result = [
undefined,
undefined,
{
group: '1042000018356',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ce4e5e6dcf8f9eff7b2fff3f1" rel="noreferrer noopener nofollow">[email protected]</a>'
},
{
group: '1042000098595',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34555657745051475f1a575b59" rel="noreferrer noopener nofollow">[email protected]</a>'
}
]

// This my logic:


result = result.map(function(index){

for (var key in array) { // Looping
if( index == key ){
var group = array[key]; // Group id
var email = key; // Email
return {group , email};
}
}

});
console.log("result", result);

如何从对象中删除未定义的值

谁能帮帮我吗?

最佳答案

您可以执行一个过滤器并传递一个 bool 构造函数,这将确保您从结果数组中删除所有 null 和未定义的值

var result  = [
undefined,
undefined,
{
group: '1042000018356',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9be3e2e1dbfffee8f0b5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a>'
},
{
group: '1042000098595',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e9eaebc8ecedfbe3a6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a>'
}
]

result = result.filter(Boolean);

console.log(result);

关于javascript - 从对象中删除未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61577274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com