我有一个名为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",
},
]
}
]
我需要做的是…我想把所有的答案按"视频"分类。在开始和之后输入"audio"然后输入& 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) => {}
的返回值应该返回值>0如果ele1
应该放在ele2
之后,一个值<如果ele1
应该放在ele2
之前,则为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;
});
应该使用lodash。查看_.orderBy()方法。这里有一个有用的链接:https://www.geeksforgeeks.org/lodash-_-orderby-method/
您需要添加自定义比较功能。
这是你的问题最简单的解决方法
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);
您可以轻松地通过使用JS将比较器函数传递给sort
函数来实现结果
"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);