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

103
Views
Group objects by key order

Formulating this problem is a bit complicated so bear with me.

I'm creating a Frequently Asked Questions (FAQ) component, the records are given as a list of objects, like below:

[
    { "slice_type": "information" },

    /* Question 1 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "quote" },
    { "slice_type": "content" },

    /* Question 2 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "image" },
    { "slice_type": "youtube_embed" }
    ...
]

As you know in a collapsible FAQ, the question is always on the header bloc while the rest of the content lays in the body of the component.

Representation of a collapsible FAQ

Thus is why I need to group the answers of each questions in a list, I will then be able to fetch the desired answers by the question index, here's what I'd like to get.

[
    [
        { "slice_type": "content" },
        { "slice_type": "quote" },
        { "slice_type": "content" },
    ], 
    [
        { "slice_type": "content" },
        { "slice_type": "image" },
        { "slice_type": "youtube_embed" }
    ],
    ...
]

I'd like to know how I can achieve this result programatically?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Can be done with a forEach(). Since you might have items before your questions actually start(as shown in sample data) you should start your index with -1.

let items = [
    { "slice_type": "information" },

    /* Question 1 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "quote" },
    { "slice_type": "content" },

    /* Question 2 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "image" },
    { "slice_type": "youtube_embed" }
  
]

let groupedItems = [];
let quesIndex = -1;
items.forEach((x) => {
   if(x.slice_type == 'question' ){
     groupedItems.push([]);
     quesIndex++;
   }
   else if(quesIndex!=-1){
     groupedItems[quesIndex].push(x);
   }
});

console.log(groupedItems);

about 4 years ago · Juan Pablo Isaza Report

0

Using array#reduce,

You can iterate through the array and for each item check if the slice_type property is equals to "question"

  • if create a new array for that question,
  • if not add the object to the array of the previous question.

Like so:

let newData = data.reduce((acc, obj) => {
  if (obj.slice_type == "question") acc.push([])
  else acc[acc.length - 1]?.push(obj)
  
  return acc
}, [])

Demo:

let data = [
    {"slice_type": "dataBeforeQuestion"},

    /* Question 1 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "quote" },
    { "slice_type": "content" },

    /* Question 2 */
    { "slice_type": "question" },
    { "slice_type": "content" },
    { "slice_type": "image" },
    { "slice_type": "youtube_embed" }
]

let newData = data.reduce((acc, obj) => {
  if (obj.slice_type == "question") acc.push([])
  else acc[acc.length - 1]?.push(obj)
  
  return acc
}, [])

console.log(newData)
.as-console-wrapper {min-height: 100%!important; top: 0;}

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!