使用JavaScript删除JSON部分时删除空值

发布于 2025-01-28 03:24:11 字数 2391 浏览 4 评论 0原文

我在下面有一个JSON对象。如果问题值为空,我想删除零件。因此,根据JSON下面的说法,我需要删除ID = 7602部分。

 [   {
                "id": 9333,
                "component": "question_pool",
                "sub_comp_arr": [
                    {
                        "id": 7769,
                        "component": "question",
                        "sub_comp_arr": [
                            {
                                "id": 2552,
                                "component": "question_segment",
                                "value": "Answer1"
                            },
                            {
                                "id": 1011,
                                "component": "question_segment",
                                "value": "Answer2"
                            },
                            {
                                "id": 8691,
                                "component": "question_segment",
                                "value": "Answer3"
                            }
                        ],
                        "type": "single_choice",
                        "value": "<p>Question1?</p>\n"
                    },
                    {
                        "id": 7602,
                        "component": "question",
                        "sub_comp_arr": [
                            {
                                "id": 921,
                                "component": "question_segment",
                                "value": ""
                            }
                        ],
                        "type": "single_choice",
                        "value": ""
                    }
                ]
            },{...}
    ]

我已经将代码插入如下,

    var y= content_json.content_arr;
    var keyCount  = Object.keys(y).length;
    for (var i = 0; i < keyCount; i++) {    
        var questionCount = (content_json.content_arr[i]['sub_comp_arr']).length;
        for (let j = 0; j < questionCount; j++){    
          var emptyquestion= ((content_json.content_arr[i]['sub_comp_arr'][j]['value']).trim()).length;
          if (emptyquestion===0){
            delete (content_json.content_arr[i]['sub_comp_arr'][j]);
}
    
    }
        
    }

但是问题是如果我使用delete(content_json.content_arr [i] ['sub_comp_arr'] [j])[j]);它在我的json上保存一个null值我不要。如何实现它

I am having an json object below. I want to delete a part if the question value is empty. So according to below json I need to delete the id=7602 portion.

 [   {
                "id": 9333,
                "component": "question_pool",
                "sub_comp_arr": [
                    {
                        "id": 7769,
                        "component": "question",
                        "sub_comp_arr": [
                            {
                                "id": 2552,
                                "component": "question_segment",
                                "value": "Answer1"
                            },
                            {
                                "id": 1011,
                                "component": "question_segment",
                                "value": "Answer2"
                            },
                            {
                                "id": 8691,
                                "component": "question_segment",
                                "value": "Answer3"
                            }
                        ],
                        "type": "single_choice",
                        "value": "<p>Question1?</p>\n"
                    },
                    {
                        "id": 7602,
                        "component": "question",
                        "sub_comp_arr": [
                            {
                                "id": 921,
                                "component": "question_segment",
                                "value": ""
                            }
                        ],
                        "type": "single_choice",
                        "value": ""
                    }
                ]
            },{...}
    ]

I have implemet the code as below

    var y= content_json.content_arr;
    var keyCount  = Object.keys(y).length;
    for (var i = 0; i < keyCount; i++) {    
        var questionCount = (content_json.content_arr[i]['sub_comp_arr']).length;
        for (let j = 0; j < questionCount; j++){    
          var emptyquestion= ((content_json.content_arr[i]['sub_comp_arr'][j]['value']).trim()).length;
          if (emptyquestion===0){
            delete (content_json.content_arr[i]['sub_comp_arr'][j]);
}
    
    }
        
    }

But the problem is if I use delete (content_json.content_arr[i]['sub_comp_arr'][j]); It is saving a null value on my Json, Which I don't want. How to achieve it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2025-02-04 03:24:11

您可以使用过滤器。

content_json.content_arr[i].sub_comp_arr = content_json.content_arr[i].sub_comp_arr.filter(q => q.value)

You could use filter instead.

content_json.content_arr[i].sub_comp_arr = content_json.content_arr[i].sub_comp_arr.filter(q => q.value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文