javascript - 如何根据子字段的值对对象数组进行排序?

标签 javascript arrays sorting arraylist lodash

我有一个名为 posts 的对象数组,其中包含嵌套字段答案。我有不同的条件,具体取决于我需要对数组进行排序。

数组示例

let posts = [
  {
                        "_id": "610b806ec6386ffa276b6d1c",
                        "post": "CDat the difference between Equity and News channel?",
                        "answers": [
                            {
                                "_id": "613724604dd4b6f39b344b4c",
                                "type": "text",
                                
                            },
                            {
                                "_id": "6113c826a64da80fe48ab543",
                                "type": "video",
                                
                            },
                            {
                                "_id": "6113c740a64da80fe48ab534",
                                "type": "audio",
                                
                            },
                            {
                                "_id": "611135cf7497d70b6cc2c5e0",
                                "type": "video",
                               
                            }
                        ]
                    },
                    {
                        "_id": "611e14329ff0c343540ae10e",
                        "post": "How forex affects investment 6",
                        "answers": [
                            {
                                "_id": "61371b3a9bf14a39207fff8a",
                                "type": "video",
                                
                            },
                            {
                                "_id": "613719369507fd12f93e3c62",
                                "type": "text",
                                
                            },
                            {
                                "_id": "6135f28e29aeae3de45fc8c2",
                                "type": "text",
                                
                            },
                            {
                                "_id": "6135f07c831da33c28fc1cf6",
                                "type": "audio",
                                
                            },
                            {
                                "_id": "6135eb2d51a8830698d65cf3",
                                "type": "text",
                               
                            },
                           
                        ]
                    }

]

我需要做的是......我想对所有类型为“视频”的答案进行排序,然后输入“音频”,然后输入“文本”答案,如下所示


let posts = [
  {
                        "_id": "610b806ec6386ffa276b6d1c",
                        "post": "CDat the difference between Equity and News channel?",
                        "answers": [
                            {
                                "_id": "613724604dd4b6f39b344b4c",
                                "type": "video",
                                
                            },
                            {
                                "_id": "6113c826a64da80fe48ab543",
                                "type": "video",
                                
                            },
                            {
                                "_id": "6113c740a64da80fe48ab534",
                                "type": "audio",
                                
                            },
                            {
                                "_id": "611135cf7497d70b6cc2c5e0",
                                "type": "text",
                               
                            }
                        ]
                    },
                    {
                        "_id": "611e14329ff0c343540ae10e",
                        "post": "How forex affects investment 6",
                        "answers": [
                            {
                                "_id": "61371b3a9bf14a39207fff8a",
                                "type": "video",
                                
                            },
                            {
                                "_id": "613719369507fd12f93e3c62",
                                "type": "audio",
                                
                            },
                            {
                                "_id": "6135f28e29aeae3de45fc8c2",
                                "type": "text",
                                
                            },
                            {
                                "_id": "6135f07c831da33c28fc1cf6",
                                "type": "text",
                                
                            },
                            {
                                "_id": "6135eb2d51a8830698d65cf3",
                                "type": "text",
                               
                            },
                           
                        ]
                    }

]

如有任何帮助,我们将不胜感激。谢谢

最佳答案

您需要循环遍历 posts 数组中的每个条目,并使用自定义函数对每个条目内的 answers 数组进行排序。

如果 ele1 应放置在 ele2< 之后,则自定义函数 (ele1, ele2) => {} 的返回值应返回 > 0 的值,如果 ele1 应放置在 ele2 之前,则值 < 0;如果认为它们相等,则值 = 0。

const getRanking = (ele) => {
    if (ele.type == "video") return 1; 
    if (ele.type == "audio") return 2;
    if (ele.type == "text") return 3;
};

posts = posts.map(post => {
    post.answers.sort((ele1, ele2) => {
        return getRanking(ele1) - getRanking(ele2);
    });
    return post;
}); 

关于javascript - 如何根据子字段的值对对象数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69098042/

相关文章:

javascript - 如何像 Angular 5 那样从 FB 页面获取响应

javascript - jQuery 中的 keyevent 不起作用

sorting - Redis 基于分数和日期时间排序的集合作为决胜局?

c++ - 在特定情况下无法向下渗透 MIN 二进制堆

javascript - SessionStorage 浏览器范围(范围)或 LocalStorage 在退出时删除

javascript - 使用 PhoneGap 时从 iframe 调用父 jQuery 函数

C++ : recognizing lower case as correct when its stored as upper case?

python 字节数组

javascript - 如何将数组放在文本区域中,将每个元素放在自己的行中?

c++ - 使用迭代器对 std::list 进行排序