删除对象/数组中的所有对象

时间:2018-08-01 07:53:20

标签: javascript

您能帮我提供删除数组中所有对象的代码吗?

在我的示例中,我具有包含对象的数组。

每个对象都包含键,我想查看每个键并检查它是否为对象。

如果是对象,我想删除该键(在我的示例中,我只是将其值重写为空字符串)

   if(typeof result2.length == 'number'){
    for(var u in result2){
    Object.keys(result2[u])
.filter(k => typeof result2[u][k] === 'object')
.map(k => delete result2[u][k])
}
}else{
    Object.keys(result2)
.filter(k => typeof result2[k] === 'object')
.map(k => delete result2[k])
}

如果result2包含更多对象,我的代码将起作用。

如果仅包含1个对象,则不起作用。

如果输入仅包含1个对象,则其从数组到对象的变化。

1 个答案:

答案 0 :(得分:0)

SELECT C.username UserQ , R.username UserA , C.commentID , D.Title , C.text Comment FROM Comments C JOIN Detail D ON C.vID = D.vId JOIN Search S ON C.vID = S.vID JOIN Replies R ON C.commentID = R.CommentID_ParentID WHERE D.categoryId = 25 AND S.searched_keyword = 'stackoverflow' GROUP BY UserQ , UserA , commentID , Title , Comment Current Outcome: UserQ UserA commentID Title Comment Sven Katja 213 Need Help Hi, I need help Sven Sven 214 Answer1 Hi, I need help Sven Ben 215 Answer2 Hi, I need help Tess NULL 216 Soccer I love soccer Carl Tanja 216 Question Do I need SQL for jobs? Carl Peter 216 Answer Do I need SQL for jobs? 上替换Desired Outcome: UserQ UserA commentID Title Comment Sven Katja 213 Need Help Hi, I need help Sven Sven 214 Answer1 Try to solve it like.. Sven Ben 215 Answer2 You can also try... Tess NULL 216 Soccer I love soccer Carl Tanja 216 Question Do I need SQL for jobs? Carl Peter 216 Answer Yes...

您还可以做到:

result2[u][p] = "";