简体   繁体   English

ES6,Lodash sortBy通过2级对象数组

[英]ES6, Lodash sortBy 2 level array of objects

Can't figure out how to sort this kind of structure: 无法弄清楚如何对这种结构进行排序:

[
  0: { text: 'Lorem', 'children': [ 0: { text: 'Ipsum of Lorem' } ... ] }
  ...
]

I can only sort by the first level like this: _.sortBy(myArray,'text') 我只能按这样的第一级排序: _.sortBy(myArray,'text')

I need something like _.sortBy(myArray, ['text','children.text']) this obviously doesn't work. 我需要类似_.sortBy(myArray, ['text','children.text'])这显然行不通。

I need to sort by children.text of each item. 我需要按children.text对每个项目进行排序。

If you need to sort by the 1st item of the children array, you can use 'children[0].text' : 如果您需要按children数组的第一项进行排序,则可以使用'children[0].text'

 const myArray = [ { text: 'Lorem', 'children': [{ text: 'Ipsum2' }] }, { text: 'Lorem', 'children': [{ text: 'Ipsum1' }] } ] const result = _.sortBy(myArray, ['text','children[0].text']) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script> 

If you need to sort the children, and then use the 1st item, you can generate a function with _.flow() . 如果需要_.flow()项进行排序,然后使用第一项,则可以使用_.flow()生成函数。 The function starts by mapping each object, and sorting it's children , and then sorts the entire array: 该函数首先映射每个对象并对其children对象进行排序,然后对整个数组进行排序:

 const { flow, partialRight: pr, map, sortBy } = _ const fn = flow( pr(map, o => ({ ...o, children: sortBy(o.children, 'text') })), pr(sortBy, ['text','children[0].text']) ) const myArray = [ { text: 'Lorem', 'children': [{ text: 'Ipsum2' }, { text: 'Ipsum4' }] }, { text: 'Lorem', 'children': [{ text: 'Ipsum3' }, { text: 'Ipsum1' }] } ] const result = fn(myArray) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script> 

And the same idea but terser with lodash/fp: 和相同的想法,但与lodash / fp相同:

 const { flow, map, sortBy } = _ const fn = flow( map(o => ({ ...o, children: sortBy('text', o.children) })), sortBy(['text','children[0].text']) ) const myArray = [ { text: 'Lorem', 'children': [{ text: 'Ipsum2' }, { text: 'Ipsum4' }] }, { text: 'Lorem', 'children': [{ text: 'Ipsum3' }, { text: 'Ipsum1' }] } ] const result = fn(myArray) console.log(result) 
 <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script> 

You should loop over the children array. 您应该遍历children数组。

 const myArray = [{ text: 'ZLorem', 'children': [{ text: 'Ipsum2' }, { text: 'Apsum2' }] },{ text: 'Lorem', 'children': [{ text: 'Ipsum1' }, { text: 'Zpsum1' }] }], result = _.sortBy(myArray, 'text'); result.forEach(o => { if (o.children) o.children = _.sortBy(o.children, 'text'); }); console.log(result); 
 .as-console-wrapper { min-height: 100%; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script> 

You can use for loop to trace the children and sort the children array. 您可以使用for循环跟踪子代并对子代数组进行排序。

_.sortBy(myArray, 'text');
for(let parent of myArray) {
    _.sortBy(parent.children, 'text');
}

This code should do like you want or you can create it as function 该代码应按您想要的方式执行,也可以将其创建为函数

function sortParentAndChildren(myArray, key) {
    _.sortBy(myArray, key);
    for (let parent of myArray) {
        _.sortBy(parent.children, key);
    }
}

sortParentAndChildren(myArray, "text");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM