Reactjs 如何根据react native中的likes_计数对地图数据进行排序? 构造函数(道具){ 超级(道具); 让评论=[{body:“good”,比如[u count:2}, {正文:“坏文章”,如_count:0}, {body:“great”,如_count:4}]; this.state={comments}; render(){ 返回( {comments.map((注释,索引)=> {comment.body} )} ); };
这是代码,我想根据like_计数映射数据,我想在顶部有最大like计数的注释,我想像这样打印整个注释 如何添加此映射的条件。 我的输出将是:body:“great”,比如:4, 身体:“很好”,比如:2, 正文:“坏文章”,如:0 如果有人知道,请建议解决方案,提前谢谢。请注意,如果您想要新数组,请在Reactjs 如何根据react native中的likes_计数对地图数据进行排序? 构造函数(道具){ 超级(道具); 让评论=[{body:“good”,比如[u count:2}, {正文:“坏文章”,如_count:0}, {body:“great”,如_count:4}]; this.state={comments}; render(){ 返回( {comments.map((注释,索引)=> {comment.body} )} ); };,reactjs,sorting,react-native,mapping,Reactjs,Sorting,React Native,Mapping,这是代码,我想根据like_计数映射数据,我想在顶部有最大like计数的注释,我想像这样打印整个注释 如何添加此映射的条件。 我的输出将是:body:“great”,比如:4, 身体:“很好”,比如:2, 正文:“坏文章”,如:0 如果有人知道,请建议解决方案,提前谢谢。请注意,如果您想要新数组,请在排序之前添加.slice()。请注意,如果您想要新数组,请在排序之前添加.slice()。 constructor(props) { super(props); let comments = [{b
排序之前添加.slice()
。请注意,如果您想要新数组,请在排序之前添加.slice()
。
constructor(props) {
super(props);
let comments = [{body: "good", like_count: 2},
{body: "bad article", like_count: 0},
{body: "great", like_count: 4}];
this.state = { comments };
render() {
return(
{ comments.map((comment, index) =>
<Text>{comment.body}</Text>
)}
);
};
let comments = [
{body: "good", like_count: 2},
{body: "bad article", like_count: 0},
{body: "great", like_count: 4}
];
let sortedComments = comments.sort((a, b) => a.like_count < b.like_count)
[
{ body: 'great', like_count: 4 },
{ body: 'good', like_count: 2 },
{ body: 'bad article', like_count: 0 }
]