wwwwwllllk 发表于 2023-9-11 23:19:36

最近没有产出,分享一个面试题,希望gg们各显神通

本帖最后由 wwwwwllllk 于 2023-9-11 23:20 编辑

```javascript
const dataSource = [
{value: "1", groupId: 'a'},
{value: "15", groupId: 'b'},
{value: "7", groupId: 'a'},
{value: "9", groupId: 'c'},
{value: "5", groupId: 'b'},
{value: "11"}
];

把上面的dataSource数组输出为

[
[ { value: '1', groupId: 'a' }, { value: '7', groupId: 'a' } ],
[ { value: '5', groupId: 'b' }, { value: '15', groupId: 'b' } ],
[ { value: '9', groupId: 'c' } ],
[ { value: '11' } ]
]
```

gg们可以给自己限时10分钟,然后不要借助任何工具就自己手写

JKstring 发表于 2023-9-11 23:51:47

本帖最后由 JKstring 于 2023-9-12 00:47 编辑

const dataSource = [
    { value: "1", groupId: 'a' },
    { value: "15", groupId: 'b' },
    { value: "7", groupId: 'a' },
    { value: "9", groupId: 'c' },
    { value: "5", groupId: 'b' },
    { value: "11" }
];

const formattedData = dataSource.sort((a, b) => {
    if (a.groupId < b.groupId) {
      return -1;
    } else if (a.groupId > b.groupId) {
      return 1;
    } else {
      return a.value - b.value;
    }
}).reduce((acc, item) => {
    const group = acc.find(group => group.groupId === item.groupId);

    if (group) {
      group.push(item);
    } else {
      acc.push();
    }

    return acc;
}, []);

console.log(formattedData);

steven026 发表于 2023-9-12 00:48:14

const dataSource = [
{value: "1", groupId: 'a'},
{value: "15", groupId: 'b'},
{value: "7", groupId: 'a'},
{value: "9", groupId: 'c'},
{value: "5", groupId: 'b'},
{value: "11"}
];
const temp=dataSource.reduce((obj,data)=>{
    const {groupId}=data
    obj=obj||[]
    obj.push(data)
    return obj
},{})
Object.values(temp)

wwwwwllllk 发表于 2023-9-12 08:33:12

steven026 发表于 2023-9-12 00:48


每个value也要从小到大排列

steven026 发表于 2023-9-12 08:57:29

wwwwwllllk 发表于 2023-9-12 08:33
每个value也要从小到大排列

Object.values(temp).map(i=>i.sort((a,b)=>a.value-b.value))

王一之 发表于 2023-9-12 11:34:23

gpt-4来咯

```js
const dataSource = [
{value: "1", groupId: 'a'},
{value: "15", groupId: 'b'},
{value: "7", groupId: 'a'},
{value: "9", groupId: 'c'},
{value: "5", groupId: 'b'},
{value: "11"}
];

let result = dataSource.reduce((acc, cur) => {
let targetIndex = acc.findIndex(arr => arr.groupId === cur.groupId);

if (targetIndex !== -1) {
    acc.push(cur);
} else {
    acc.push();
}

return acc;
}, []);

console.log(result);
```

王一之 发表于 2023-9-12 11:48:23

脚本猫ide写的

```js
    const dataSource = [
      { value: "1", groupId: 'a' },
      { value: "15", groupId: 'b' },
      { value: "7", groupId: 'a' },
      { value: "9", groupId: 'c' },
      { value: "5", groupId: 'b' },
      { value: "11" }
    ];

    let result = [];

    let empty = [];

    for (let i = 0; i < dataSource.length; i++) {
      let val = dataSource;
      if (!val.groupId) {
            empty.push(val);
            continue;
      }
      let index = val.groupId.charCodeAt(0) - 97;
      if (!result) {
            result = [];
      }
      result.push(val);
    }
    result.push(empty);
    for (let i = 0; i < result.length; i++) {
      console.log(result);
      result = result.sort((a, b) => a.value - b.value);
    }

    console.log(result);
```

Yiero 发表于 2023-9-12 13:31:36

本帖最后由 Yiero 于 2023-9-12 13:40 编辑

```js
const dataSource = [
        { value: "1", groupId: 'a' },
        { value: "15", groupId: 'b' },
        { value: "7", groupId: 'a' },
        { value: "9", groupId: 'c' },
        { value: "5", groupId: 'b' },
        { value: "11" }
];

dataSource.sort( ( a, b ) => a.value - b.value );
const dataMap = new Map();
dataSource.forEach( ( data ) => {
        const groupArray = dataMap.get( data.groupId ) || dataMap.set( data.groupId, [] ).get( data.groupId );
        groupArray.push( data );
} )
const result = Array.from( dataMap.values() );
console.log( result );
```

Yiero 发表于 2023-9-24 17:11:55

这几天刚学的新东西, 实验性内容(

```js
const dataSource = [
        { value: "1", groupId: 'a' },
        { value: "15", groupId: 'b' },
        { value: "7", groupId: 'a' },
        { value: "9", groupId: 'c' },
        { value: "5", groupId: 'b' },
        { value: "11" }
];

const result = Object.values(
    Object.groupBy(
      dataSource.toSorted( ( a, b ) => a.value - b.value ),
      ( item ) => item.groupId
    )
);

console.log( result );
```
页: [1]
查看完整版本: 最近没有产出,分享一个面试题,希望gg们各显神通