Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Oh man groupBy() and arrayToObject() are breaking me

So I have been working on some extra credit for my classes. I am very new to programming and have already sought help for this same assignment, I started rolling through and now am absolutely lost.

I need to define the two functions groupBy() and arrayToObect() as asked in the below test.

I'm not necessarily looking for the answer but if someone could help point me in the right direction that would be awesome.

What I have deduced is as follows:

  1. I need to be using the spread operator ...

  2. I need to create a newObj = {}

    a. and somehow push the element derived from the array into the obj

  3. I need to take the individual values of the array and assign them as keys, with the variances as the properties of the key.

  4. Bracket notation

I have been racking my brain for hours on this now and could really use some guidance!

describe('groupBy', function () {
        const input = [4.2, 6.1, 6.3]
        const result = groupBy(input, (el) => Math.floor(el))

        it('returns an object', function () {
            expect(result).to.be.an('object')
        })

        it('group array items together based on the callback return value', function () {
            expect(result).to.be.eql({
                4: [4.2],
                6: [6.1, 6.3],
            })
        })
    })

    describe('arrayToObject', function () {
        const input = ['cat', 'dog', 'bird']
        const result = arrayToObject(input, (word) => word + 's')

        it('returns an object', function () {
            expect(result).to.be.an('object')
        })

        it('object has original array elements as keys and the result of the callback as values', function () {
            expect(result).to.be.eql({
                cat: 'cats',
                dog: 'dogs',
                bird: 'birds',
            })
        })
    })
})

groupBy
Write a function called groupBy which takes an array and a callback. The function should return an object. Each return value of the callback should be a key of the object and the values should be the input element with which the callback was called.

arrayToObject
Write a function called arrayToObject which takes an array and a callback. The function should return an object. Each element of the input array should be a key of the returned object and the output from the callback with an element passed in as the corresponding value.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

These questions have been answered a million times on stackoverflow. Essentially what you want to be doing here is using the common js array functions map, filter, reduce, flatten, ..., and think about how your problem can be expressed in terms of those.

A lot of real world code is transforming data like this, so it's good to be comfortable doing it.

Also realize that spread syntax copies the entire object which can be pretty inefficient. JavaScript doesn't have persistent data structures! It's usually better to just mutate — as long as your code is what "owns" the object.

const groupBy = (elts, keyfn) =>
  elts.reduce((m, elt) => {
    const key = keyfn(elt);
    m[key] = m[key] || [];
    m[key].push(elt);
    return m;
  }, {});
const arrayToObject = (elts, fn) =>
  elts.reduce(
    (obj, elt) => Object.assign(obj, { [elt]: fn(elt) }),
    {},
  );
about 4 years ago · Juan Pablo Isaza Report

0

I figured it out using a for loop!!

function groupBy(arr, callback) {
    const newObj = {}

    for (let i = 0; i < arr.length; i++) {
        if (callback(arr[i])) {
            const key = callback(arr[i])
            newObj[key] = newObj[key] || []
            newObj[key].push(arr[i])
        }
    }
    return newObj
}
function arrayToObject(arr, callback) {
    const obj = {}

    for (let i = 0; i < arr.length; i++) {
        if (callback(arr[i])) {
            const key = callback(arr[i])

            obj[arr[i]] = obj[key] || callback(arr[i])
        }
    }
    return obj
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!