简体   繁体   English

JS:仅对对象数组的一个字段进行排序

[英]JS: Sort only one field of an array of objects

I have the next array structure:我有下一个数组结构:

const arr = [
   {
      label: 'Apple',
      data: [{test: '1'}, {test: '2'}],
      collapsedData: [{test: '1'}, {test: '2'}],

   },
   {
      label: 'Orange',
      data: [{test: '3'}, {test: '4'}],
      collapsedData: [{test: '3'}, {test: '4'}],
   },
   {
      label: 'Tomato',
      data: [{test: '5'}, {test: '6'}],
      collapsedData: [{test: '5'}, {test: '6'}],

   },
   {
      label: 'Banana',
      data: [{test: '7'}, {test: '8'}],
      collapsedData: [{test: '7'}, {test: '8'}],
   }
]

I also have a method to sort an array - sortArr - sorts an array by alphabet.我还有一种对数组进行排序的方法 - sortArr - 按字母对数组进行排序。

I need to iterate over arr and apply sortArr to data and collapsedData if the label is Apple .如果 label 是Apple ,我需要遍历arr并将sortArr应用于datacollapsedData

So, data and collapsedData should be sorted for Apple label.因此,应为Apple label 对datacollapsedData进行排序。

Desired output:所需的 output:

const outputArr = [
   {
      label: 'Apple',
      data: [{test: '2'}, {test: '1'}],
      collapsedData: [{test: '2'}, {test: '1'}],

   },
   {
      label: 'Orange',
      data: [{test: '3'}, {test: '4'}],
      collapsedData: [{test: '3'}, {test: '4'}],
   },
   {
      label: 'Tomato',
      data: [{test: '5'}, {test: '6'}],
      collapsedData: [{test: '5'}, {test: '6'}],

   },
   {
      label: 'Banana',
      data: [{test: '7'}, {test: '8'}],
      collapsedData: [{test: '7'}, {test: '8'}],
   }
]

What can be the solution?有什么解决办法?

Take a look on below:看看下面:

function sortExceptApples(arr){
    for (let i = 0; i < arr.length; i++)
      if (arr[i].label !== "Apple")
        arr[i] = { ...arr[i], data: sortArr(arr[i].data), collapsedData: sortArr(arr[i].collapsedData) }
 return arr
}

and then:接着:

let outputArr = sortExceptApples(arr)

As per your question根据你的问题

I need to iterate over arr and apply sortArr to data and collapsedData if the label is Apple.如果 label 是 Apple,我需要遍历arr并将 sortArr 应用于 data 和 collapsedData。

  1. First filter your arr with arr.filter(x => x.label == 'Apple') to get only Apple record.首先使用arr.filter(x => x.label == 'Apple')过滤您的arr以仅获取Apple记录。
  2. Then loop over it with forEach and sort your inner object data & collapsedData然后用forEach循环它并对你的内部 object datacollapsedData数据进行排序
  3. Keep in mind that data is array of object , so need to filter with its property value like a.test .请记住, dataobjectarray ,因此需要使用其属性值进行过滤,例如a.test I've attached + before a.test which will convert value to number .我在a.test之前附加了+ ,它将 value 转换为number

You can check output below.您可以在下面查看 output。

 const arr = [ { label: 'Apple', data: [{test: '2'}, {test: '1'}], collapsedData: [{test: '2'}, {test: '1'}], }, { label: 'Orange', data: [{test: '3'}, {test: '4'}], collapsedData: [{test: '3'}, {test: '4'}], }, { label: 'Tomato', data: [{test: '5'}, {test: '6'}], collapsedData: [{test: '5'}, {test: '6'}], }, { label: 'Banana', data: [{test: '7'}, {test: '8'}], collapsedData: [{test: '7'}, {test: '8'}], } ]; const t = () => { arr.filter(x => x.label == 'Apple').forEach(x => { x.data.sort((a,b) => +a.test - +b.test); x.collapsedData.sort((a,b) => +a.test - +b.test); }); return arr; } console.log(t());

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

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