简体   繁体   中英

How can I approach and align elements in the array?

I already tried:

let age = arr.sort(function(a, b) {
    return b -a;
});

it was good for simple array, but it does not work. In this array:

let list = [
  [
    ['firstName', 'Joe'],
    ['age', 42],
    ['gender', 'male'],
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['gender', 'female'],
  ],
  [
    ['lastName', 'Kim'],
    ['age', 40],
    ['gender', 'female'],
  ],
];

it's double array, and 'age' index locate is different. How can I approach the 'age' and align? I want to sort ascending.

You can use fromEntries to convert your array item into object and then sort. But you should consider updating the item to object to avoid this unnecessary conversion.

 const list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ]; list.sort((a, b) => Object.fromEntries(a).age - Object.fromEntries(b).age); console.log(list);

You can do the following by finding the index of the child array where we can find age. From the example we can see that age is at index 0 and the age value is at index 1 of the sub array. You can than compare the age values.

 list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ]; res = list.sort((a, b) => { ageIndexA = a.findIndex(item => item[0] ==='age'); ageIndexB = b.findIndex(item => item[0] === 'age'); return a[ageIndexA][1] - b[ageIndexB][1]; }); console.log(res);

The following should work:

 let list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ]; const compareFunction = (a,b) => { return a.age - b.age; } const sortList = (arr) => { const objectList = arr.map(e => Object.fromEntries(e)); return objectList.sort(compareFunction); } console.log('sorted:', sortList(list));

The following will sort your array according to ascending age:

 let list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ]; let age = list.sort(function(a, b) { return a.filter(aa=>aa[0]=="age")[0][1] -b.filter(bb=>bb[0]=="age")[0][1];}) console.log(age) // this is a better approach: let listo=list.map(e=>e.reduce((a,[k,v])=>(a[k]=v,a), {})); console.log(listo.sort((a,b)=>a.age-b.age))

However, it would be better to store your data in a different format:. My second part generates such a format in listo . The sorting function is then trivial again ( (a,b)=>a.age-b.age ):

[
  {
    "firstName": "Mary",
    "lastName": "Jenkins",
    "age": 36,
    "gender": "female"
  },
  {
    "lastName": "Kim",
    "age": 40,
    "gender": "female"
  },
  {
    "firstName": "Joe",
    "age": 42,
    "gender": "male"
  }
]
``´

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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