Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created January 14, 2019 08:44
Show Gist options
  • Save abel-masila/4d47d6cd12fa678a2cf71cf84e019470 to your computer and use it in GitHub Desktop.
Save abel-masila/4d47d6cd12fa678a2cf71cf84e019470 to your computer and use it in GitHub Desktop.
const data={
variants: [
{
id: 'ae020c58-5d83-4665-9e5f-af3f21f80371',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '7',
color: 'pink'
},
slug: null
},
{
id: '0106daa9-c355-4699-8502-319fec8a7ca9',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '7',
color: 'gree'
},
slug: null
},
{
id: '0425703f-69f9-493d-bf45-f0ebb13620bc',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '7',
color: 'red'
},
slug: null
},
{
id: '19b73ae6-a7ba-4c2d-bda3-87037b0b1ab1',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '6',
color: 'pink'
},
slug: null
},
{
id: '1f193290-600d-4b30-a634-1234735b4ab3',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '6',
color: 'gree'
},
slug: null
},
{
id: 'd060a0a7-1062-4d10-844a-1d98b894b4dc',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '6',
color: 'red'
},
slug: null
},
{
id: 'e7d2390e-2cbf-4778-8242-f149d3975d63',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '5',
color: 'pink'
},
slug: null
},
{
id: '1341e073-e723-4173-8566-fc63bc37f7a8',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '5',
color: 'gree'
},
slug: null
},
{
id: '3df810e8-bb80-4dcc-aa98-c4185976bb5d',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '5',
color: 'red'
},
slug: null
},
{
id: '494149b8-44bf-4781-b93d-03a94e0da0d4',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '4',
color: 'pink'
},
slug: null
},
{
id: '5d5423c2-e18b-41b2-83df-d8ea2571b3cc',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '4',
color: 'gree'
},
slug: null
},
{
id: '1d0873d0-7e58-4df8-b586-19a4e0da53e3',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '4',
color: 'red'
},
slug: null
},
{
id: '4f1107aa-a944-4423-a04a-b7691a0c6a22',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '3',
color: 'pink'
},
slug: null
},
{
id: '8bfc0e88-4fd5-4578-b319-da9909f848fb',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '3',
color: 'gree'
},
slug: null
},
{
id: '383c970e-6158-4f78-a5e4-1da700c113c5',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '3',
color: 'red'
},
slug: null
},
{
id: '54c6efe4-1d52-4111-a933-e8fe745ac228',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '2',
color: 'pink'
},
slug: null
},
{
id: '24946e17-9759-4c9d-b627-eb36a9da913a',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '2',
color: 'gree'
},
slug: null
},
{
id: 'd1c75b28-52e1-44a8-92e8-42e79a7618ca',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '2',
color: 'red'
},
slug: null
},
{
id: 'dc04dabd-3e24-4a4c-8ab4-45bcc69f26ac',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '1',
color: 'pink'
},
slug: null
},
{
id: '95369e7d-6f2d-463c-a411-62a32f91104c',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '1',
color: 'gree'
},
slug: null
},
{
id: '9703d229-5a76-446a-872f-41f08d21d38b',
product: '800402fc-b0cb-4bbe-8380-3bcee92f3bf9',
category_type: 'productvariants',
products_metadata: {},
thumbnail: null,
thumbnail_frontend: null,
quantity: 1,
attributes: {
size: '1',
color: 'red'
},
slug: null
}
],
};
@BransonGitomeh
Copy link

solution kaka

const map = {};

data.variants.map((variant) => {
  const { color, size } = variant.attributes;
  if (map[color]) {
    map[color].push(size);
  } else {
    map[color] = [size];
  }
});

console.log(JSON.stringify(map, null, '\t'));

@BransonGitomeh
Copy link

or fancier

const map = {};

[...new Set(data.variants.map(({ attributes: { color } }) => color))].map(color => map[color] = []);

data.variants.map(({ attributes: { color, size } }) => map[color].push(size));

console.log(JSON.stringify(map, null, '\t'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment