使用“reduce”操作对象并生成数组

我知道reduce 是Javascript 中非常强大的数组方法,并且看过很多示例,但无法使用它来完成下面的任务。


按年龄对人的统计对象进行分组,其中年龄差异不得大于 5,且每组最多只能有 3 人。


我已经能够使用下面的代码实现它


const immutable = require('../fixtures/inputs/input')

const edge = require('../fixtures/inputs/edge-input')

/**

 * This is the entry point to the program

 *

 * @param {any} input Array of student objects

 */



function classifier(input) {

    // console.log(input)

    // let returnedInput = []

    let newInput = JSON.parse(JSON.stringify(input));


    let exampleOutput = {


    }



    if (!Array.isArray(newInput)) {


        throw new Error("invalid")

    }

    if (newInput.length < 1) {

        exampleOutput = { noOfGroups: 0 }

    }

    function compare(a, b) {

        const { age: ageA, regNo: regNoA } = a

        const { age: ageB, regNo: regNoB } = b

        // const ageA = a.age

        // const ageB = b.age

        let comparison = 0;

        if (ageA > ageB) {

            comparison = 1;

        } else if (ageA < ageB) {

            comparison = -1;

        }

        return comparison

    }


    const ages = newInput.map(function (each) {

        let datDob = new Date(each.dob).getFullYear()

        return each.age = new Date().getFullYear() - datDob

    })


    sortedInput = newInput.sort(compare)

    // console.log(sortedInput)

    const getMember = (arg) => {

        let memArray = []

        // console.log(arg)

        if (arg.length == 1) {

            return arg

        }

        let i = 0;

        let j = 1;

        // console.log(arg)

        // console.log(arg.length)

        while (i <= arg.length) {


            while (j < 3) {

                //  console.log(arg[j])

                if (arg[j]) {

                    if ((arg[j].age - arg[i].age) <= 5) {


                        memArray.push(arg[j])

                    }

                }


                j++

            }


            memArray.push(arg[i])

            i++


            return memArray

        }


    }


    }


梵蒂冈之花
浏览 349回答 1
1回答

一只名叫tom的猫

这是我的做法:首先计算age从dob每个人。然后使用reduce()来转换数据。如果一组中存在满足给定的标准,那么,我们当前成员添加到组,重新计算sum,oldest和其它性质。如果没有,我们用当前成员创建一个新组。const input = [ { name: 'Hendrick', dob: '1853-07-18T00:00:00.000Z', regNo: '041', }, { name: 'Albert', dob: '1910-03-14T00:00:00.000Z', regNo: '033', }, { name: 'Marie', dob: '1953-11-07T00:00:00.000Z', regNo: '024', }, { name: 'Neils', dob: '1853-10-07T00:00:00.000Z', regNo: '02', }, { name: 'Max', dob: '1853-04-23T00:00:00.000Z', regNo: '014', }, { name: 'Erwin', dob: '1854-08-12T00:00:00.000Z', regNo: '09', }, { name: 'Auguste', dob: '1854-01-28T00:00:00.000Z', regNo: '08', }, { name: 'Karl', dob: '1852-12-05T00:00:00.000Z', regNo: '120', }, { name: 'Louis', dob: '1852-08-15T00:00:00.000Z', regNo: '022', }, { name: 'Arthur', dob: '1892-09-10T00:00:00.000Z', regNo: '321', }, { name: 'Paul', dob: '1902-08-08T00:00:00.000Z', regNo: '055', }, { name: 'William', dob: '1890-03-31T00:00:00.000Z', regNo: '013', }, { name: 'Owen', dob: '1853-04-26T00:00:00.000Z', regNo: '052', }, { name: 'Martin', dob: '1854-02-15T00:00:00.000Z', regNo: '063', }, { name: 'Guye', dob: '1854-10-15T00:00:00.000Z', regNo: '084', }, { name: 'Charles', dob: '1954-02-14T00:00:00.000Z', regNo: '091', }, ];// Compute age from DOB.const data = input.map(item => {&nbsp; let age = new Date().getFullYear() - new Date(item.dob).getFullYear();&nbsp; return {...item, age};});var result = data.reduce((acc, curr) => {&nbsp; let group = Object.values(acc).find(group => group.members && group.members.length < 3&nbsp;&nbsp; &nbsp; && group.members.every(member => Math.abs(member.age - curr.age) < 5));&nbsp; if (group) {&nbsp; &nbsp; group.members.push(curr);&nbsp; &nbsp; group.regNos.push(curr.regNo);&nbsp; &nbsp; group.oldest = Math.max(...group.members.map(member => member.age));&nbsp; &nbsp; group.sum = group.sum + curr.age;&nbsp;&nbsp; } else {&nbsp; &nbsp; acc.noOfGroups = acc.noOfGroups + 1 || 1;&nbsp; &nbsp; let groupName = "group" + acc.noOfGroups;&nbsp; &nbsp; acc[groupName] = {&nbsp; &nbsp; &nbsp; "members": [curr],&nbsp; &nbsp; &nbsp; "oldest": curr.age,&nbsp; &nbsp; &nbsp; "sum": curr.age,&nbsp; &nbsp; &nbsp; "regNos": [curr.regNo],&nbsp; &nbsp; };&nbsp; }&nbsp; return acc;}, {});console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript