te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I approach and align elements in the array? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I approach and align elements in the array? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Jan 16, 2021 at 11:13 Mark Rotteveel 109k227 gold badges156 silver badges220 bronze badges asked Jan 5, 2021 at 6:17 LooliiLoolii 4555 silver badges13 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 17

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 pare 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 pareFunction = (a,b) => {
  return a.age - b.age;
}
const sortList = (arr) => {
  const objectList = arr.map(e => Object.fromEntries(e));
  return objectList.sort(pareFunction);
}
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"
  }
]
``´
发布评论

评论列表(0)

  1. 暂无评论