Answer the question
In order to leave comments, you need to log in
Sorting data in react + redux?
Please tell me how to solve the problem with unnecessary sorting
1. I pull out the data from the database, they are already sorted in the right way.
The following JSON comes into React:
{"list":
{"1003":{"name":"test3"}},
{"1002":{"name":"test2"}},
{"1004":{"name":"test4"}},
{"1001":{"name":"test1"}}
}
{"list":
{"1001":{"name":"test1"}},
{"1002":{"name":"test2"}},
{"1003":{"name":"test3"}},
{"1004":{"name":"test4"}}
}
{Object.keys(this.props.data.list).map((key, index) => (
<div>{this.props.data.list[key].name}</div>
))}
Answer the question
In order to leave comments, you need to log in
In point 1, your JSON is not valid. Looks like you meant:
{"list": {
"1003":{"name":"test3"},
"1002":{"name":"test2"},
"1004":{"name":"test4"},
"1001":{"name":"test1"}
}}
{"list": [
{"1003":{"name":"test3"}},
{"1002":{"name":"test2"}},
{"1004":{"name":"test4"}},
{"1001":{"name":"test1"}}
]}
var indexed = list.reduce((acc, value) => Object.assign(acc, value), {});
var order = list.map((value) => Object.keys(value)[0]);
{this.props.data.list.order.forEach((key) => (
<div>{this.props.data.list.indexed[key].name}</div>
))}
You can normalize the data. On this occasion, I think it will be extremely useful for you to watch the second course [EN] from the creator of redux.
ps lesson about normalization , which tells how you can solve your problem, but I think to understand, you need to watch the entire course.
pps useful links:
1) https://rajdee.gitbooks.io/redux-in-russian/conten... (may be translated someday)
2) https://habrahabr.ru/company/hh/blog/ 310524/ (RU)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question