Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Last active May 17, 2022 14:16
Show Gist options
  • Save IvanAdmaers/369a3800138579ebf7d7a1afcda69830 to your computer and use it in GitHub Desktop.
Save IvanAdmaers/369a3800138579ebf7d7a1afcda69830 to your computer and use it in GitHub Desktop.
collectObjectValues

This function collects object values { a: 1, b: 2 } => [1, 2] { c: { d: 3, e: 4 } } => [3, 4]

import collectObjectValues from './collectObjectValues';
describe('collectObjectValues', () => {
it('should collect one-dimensional object values', () => {
const input = { a: 1, b: 2 };
const output = [1, 2];
expect(collectObjectValues(input)).toEqual(output);
});
it('should collect two-dimensional object values', () => {
const input = { user: { firstName: 'John', surname: 'Smith' } };
const output = ['John', 'Smith'];
expect(collectObjectValues(input)).toEqual(output);
});
it('should collect three-dimensional object values', () => {
const input = {
user: { logs: { '2022-05-16': 'Sign up', '2022-05-17': 'Sign in' } },
};
const output = ['Sign up', 'Sign in'];
expect(collectObjectValues(input)).toEqual(output);
});
it('should collect an object values with an array of objects', () => {
const input = {
users: [
{ name: 'John Smith', browser: 'Chrome' },
{ name: 'John Doe', browser: 'Firefox' },
],
};
const output = ['John Smith', 'Chrome', 'John Doe', 'Firefox'];
expect(collectObjectValues(input)).toEqual(output);
});
it('should collect primitives from arrays in object', () => {
const input = {
users: ['John', 'Tom'],
};
const output = ['John', 'Tom'];
expect(collectObjectValues(input)).toEqual(output);
});
it('should collect primitives from an array that are in another array that are in object', () => {
const input = {
users: [['John', 'Tom']],
};
const output = ['John', 'Tom'];
expect(collectObjectValues(input)).toEqual(output);
});
});
/**
* This function collects object values
* { a: 1, b: 2 } => [1, 2]
* { c: { d: 3, e: 4 } } => [3, 4]
* ! It can work with objects in array but can't with array of primitives !
*/
interface ArrayType
extends Array<
string | number | undefined | null | object | boolean | symbol | bigint
> {}
const collectObjectValues = (inputObject: object): Array<ArrayType> => {
const result: Array<ArrayType> = [];
const values = Object.values(inputObject);
values.forEach((value): void => {
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
const collectedObjectValues = collectObjectValues(value);
result.push(...collectedObjectValues);
return;
}
if (Array.isArray(value)) {
value.forEach((arrayItem): void => {
if (typeof arrayItem === 'object' && !Array.isArray(arrayItem) && arrayItem !== null) {
const collectedObjectValues = collectObjectValues(arrayItem);
result.push(...collectedObjectValues);
return;
}
if (Array.isArray(arrayItem)) {
const collectedArrayValues = collectObjectValues(arrayItem);
result.push(...collectedArrayValues);
return;
}
result.push(arrayItem);
});
return;
}
result.push(value);
});
return result;
};
export default collectObjectValues;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment