【问题标题】:How to sort an array of object depending on value of a subfield?如何根据子字段的值对对象数组进行排序?
【发布时间】:2021-11-04 22:20:22
【问题描述】:

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

示例数组

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",
                               
                            },
                           
                        ]
                    }

]

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你需要添加自定义比较方法,看看我的解决方案

标签: javascript arrays sorting arraylist lodash


【解决方案1】:

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

如果ele1应该放在ele2之后,自定义函数(ele1, ele2) => {}的返回值应该返回值> 0,如果ele1应该放在ele2之前,则返回值

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;
}); 

【讨论】:

    【解决方案2】:

    您应该为此使用 lodash。查看 _.orderBy() 方法。这是一个有用的链接:https://www.geeksforgeeks.org/lodash-_-orderby-method/

    【讨论】:

    • 是的,我看到了这个功能,但我不确定它在我的情况下是如何工作的。
    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    【解决方案3】:

    您需要添加自定义比较功能。

    这是您问题的最简单解决方案

    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",
                                   
                                },
                               
                            ]
                        }
    
    ]
    
    
    function type (t){
       if(t==="video") return 1;
       if(t==="audio") return 2;
       if(t==="text")return 3;
    }
    function comparotor(a, b){
      
    
      return type(a.type) - type(b.type)
    }
    
    posts.map(ele=>{
      ele.answers.sort(comparotor);
    })
    
    console.log(posts);

    【讨论】:

    • 这仅适用于 2 案例视频和文本
    • 你能再检查一下吗?
    【解决方案4】:

    只需将比较器函数传递给sort函数即可使用JS轻松实现结果

    "video" => "v"
    "audio" => "a"
    "text"  => "t"
    

    您可以使用indexOf 来检查排序字母列表中的第一个字母(vat)

    posts.map((o) =>o.answers.sort((a, b) => "vat".indexOf(a.type[0]) - "vat".indexOf(b.type[0])))
    

    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",
          },
        ],
      },
    ];
    
    const result = posts.map((o) =>o.answers.sort((a, b) => "vat".indexOf(a.type[0]) - "vat".indexOf(b.type[0])));
    console.log(result);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-14
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2019-06-22
      • 2017-06-15
      • 1970-01-01
      相关资源
      最近更新 更多