[英]How to sort a nested array using lodash
I have a collection of objects in this array and I need to order them by the 'order' key (asc). 我在这个数组中有一组对象,我需要通过'order'键(asc)对它们进行排序。 Is there a way to sort the objects inside the array and then return the whole array? 有没有办法对数组中的对象进行排序,然后返回整个数组? I am relying on the order as I'm using it in a v-for with a :key. 我依靠订单,因为我在一个v-for中使用它与:key。
[
{
"id":0,
"type":"Header",
"order":1,
"props":{
"order":0,
"id":0,
"section_id":0
},
"data":{
"header":""
},
"component":"header-block"
},
{
"id":1,
"type":"Header",
"order":0,
"props":{
"order":1,
"id":1,
"section_id":0
},
"data":{
"header":""
},
"component":"header-block"
}
],
[
//Another collection of objects
]
I am currently doing this - 我目前正在这样做 -
getters: {
sorted: state => {
return _.orderBy(state.experience_sections, function(block) {
if(block.experience_blocks[0]) {
return block.experience_blocks[0].order;
}
});
}
}
The solution above does not seem to order the objects by 'asc' order. 上面的解决方案似乎没有按'asc'顺序对对象进行排序。 Am I on the right track? 我是在正确的轨道上吗?
Thanks! 谢谢!
PS Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. PS Stack告诉我这可能是一个重复的问题,但是经过几个小时的搜索我不知所措。 My apologies if I missed an already answered question. 如果我错过了已经回答的问题,我很抱歉。
Just in case you want plain javascript solution.. using Array.forEach
以防万一你需要简单的javascript解决方案..使用Array.forEach
I have also extended your array to contain more data 我还扩展了您的数组以包含更多数据
var arr = [[ { "id":0, "type":"Header", "order":1, "props":{ "order":0, "id":0, "section_id":0 }, "data":{ "header":"" }, "component":"header-block" }, { "id":1, "type":"Header", "order":0, "props":{ "order":1, "id":1, "section_id":0 }, "data":{ "header":"" }, "component":"header-block" } ], [ { "id":0, "type":"Header", "order":2, "props":{ "order":0, "id":0, "section_id":0 }, "data":{ "header":"" }, "component":"header-block" }, { "id":1, "type":"Header", "order":1, "props":{ "order":1, "id":1, "section_id":0 }, "data":{ "header":"" }, "component":"header-block" } ]] arr.forEach(d => d.sort((a,b) => a.order - b.order)) console.log(arr)
将对数组中的每个子数组进行排序
const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));
You should also consider orderBy
method from lodash since you could easily change from asc
to desc
sort order if you would want to at a later date or have it via a variable being passed through the UI etc: 您还应该考虑来自lodash的orderBy
方法,因为您可以轻松地从asc
更改为desc
排序顺序,如果您希望以后更新或通过通过UI传递的变量等等:
const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ] console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Deep sorting using lodash 使用lodash进行深度分类
const sortedArray = _.orderBy(items, [(item) => {
const nestedObj = _.get(item, 'props');
item['props'] = _.orderBy(nestedObj,'order','desc');
return item['order'];
}], 'desc');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.