2

Previously, structure of my array was:

[
  {
    "id": "Ddfwe23224533",
    "kind": "PEAK_EXPERIENCE",
    "title": "IP - Collective Identity",
  },
  {
    "id": "Ddfwe23224534",
    "kind": "PEAK_EXPERIENCE",
    "title": "IP - Collective Identity 2",
  },
  .....
]

I have created a function called flatToHierarchy to group the elements in an array. Here is the code:

export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
  const items = accumulator;
  const propValue = currentItem[prop];
  items[propValue] = items[propValue]
    ? [...items[propValue], currentItem]
    : [currentItem];
  return items;
}, {});

Now if I call it like:

flatToHierarchy(elements, 'kind')

It works fine.


Now, structure of my array is changed. Now it looks like:

[
  {
    "entity": {
      "id": "Ddfwe23224533",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity",
    },
    entity_id: "jj98834jfj983"
  },
  {
    "entity": {
      "id": "Ddfwe23224534",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity 2",
    },
    entity_id: "jj98834jfdf83"
  },
  .....
]

So, now for grouping array by element kind, I call it this way:

flatToHierarchy(elements, 'entity.kind')

Now, it yields me something like:

{
  undefined: [....element1, ....element2 ]
}

But what I want is:

{
  PEAK_EXPERIENCE: [....element1, ....element2 ]
}

I have dug deeper in to this and found that array cannot be accessed this way:

array['something.something']

It needs to be accessed this way:

array['something']['something']

So, I changed my function but I am stuck. Here is my changed function:

export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
  const items = accumulator;
  const props = prop.split('.').map(p => [p]);
  console.log(props);
  const propValue = currentItem[prop];
  console.log(currentItem...props); // how to spread the array here?
  items[propValue] = items[propValue]
    ? [...items[propValue], currentItem]
    : [currentItem];
  return items;
}, {});

Update:

Sample of current input:

[
  {
    "entity": {
      "id": "Ddfwe23224533",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity",
    },
    entity_id: "jj98834jfj983"
  },
  {
    "entity": {
      "id": "Ddfwe23224534",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity 2",
    },
    entity_id: "jj98834jfdf83"
  },
  {
    "entity": {
      "id": "Ddfwe23224594",
      "kind": "PEAK_EXPERIENCE2",
      "title": "IP - Collective Identity 6",
    },
    entity_id: "jj98834jfdf33"
  },
]

sample of needed output:

{
  PEAK_EXPERIENCE: [
    {
      "entity": {
        "id": "Ddfwe23224533",
        "kind": "PEAK_EXPERIENCE",
        "title": "IP - Collective Identity",
      },
      entity_id: "jj98834jfj983"
    },
    {
      "entity": {
        "id": "Ddfwe23224534",
        "kind": "PEAK_EXPERIENCE",
        "title": "IP - Collective Identity 2",
      },
      entity_id: "jj98834jfdf83"
    },
  ],
  PEAK_EXPERIENCE2: [
    {
      "entity": {
        "id": "Ddfwe23224594",
        "kind": "PEAK_EXPERIENCE2",
        "title": "IP - Collective Identity 6",
      },
      entity_id: "jj98834jfdf33"
    },
  ],
}

Update 2:

While making changes to the required function, I need to keep in mind that this function will always have the signature as specified below:

flatToHierarchy(array, string);

example:

flatToHierarchy(elements, 'kind');

another example:

flatToHierarchy(elements, 'entity.kind');
2
  • This question is a bit unclear. Could you please post a sample of the input (which i think is there) and one of final output that you're looking for. Commented Sep 7, 2018 at 11:10
  • @rmn I have included the samples in update part of the question. Please have a look at them. Commented Sep 7, 2018 at 11:18

2 Answers 2

4

You're looking for a path function, which you can make by using reduce and seeding with the initial object.

Watch out for null/undefined properties!

const propValue = prop
  .split('.') // Make a list of all property names (cannot contain a .)
  .reduce((obj, p) => obj[p], currentItem); // Loop over them to traverse your object

const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
  const items = accumulator;
  const propValue = prop.split('.').reduce((obj, p) => obj[p], currentItem);
  items[propValue] = items[propValue]
    ? [...items[propValue], currentItem]
    : [currentItem];
  return items;
}, {});

const data = [
  {
    "entity": {
      "id": "Ddfwe23224533",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity",
    },
    entity_id: "jj98834jfj983"
  },
  {
    "entity": {
      "id": "Ddfwe23224534",
      "kind": "PEAK_EXPERIENCE",
      "title": "IP - Collective Identity 2",
    },
    entity_id: "jj98834jfdf83"
  },
  {
    "entity": {
      "id": "Ddfwe23224594",
      "kind": "PEAK_EXPERIENCE2",
      "title": "IP - Collective Identity 6",
    },
    entity_id: "jj98834jfdf33"
  },
]

console.log(
  flatToHierarchy(data, "entity.kind")
);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. This is what I wanted.
0

Considering your sample input is in a variable called data, you can do the following.

data = data.reduce((output, current) => {
    const kind = current.entity.kind
    output[kind] = (output[kind] || []).concat(current)
    return output
}, {})

4 Comments

thanks for the answer. But entity.kind will not be known. It will be passed to function call. Look at the update 2 part of the question.
Got it. What happens when the string is just 'kind' as in your first example. Since, there's no 'kind', just 'entity.kind', will it fail?
it will return an empty array.
For anyone referencing it in future, since the question was updated, please see the correct answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.