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

182
Views
How to get the objectives from an array

The JSON data I am working on it

 "data": [
          {
             "campaign_name": "Daily Shopping - [Reach Ad]",
             "reach": "2365439",
             "clicks": "43692",
             "impressions": "4873908",
             "actions": [
                {
                   "action_type": "like",
                   "value": "1"
                },
                {
                   "action_type": "comment",
                   "value": "40"
                 }
             ],
             "date_start": "2022-05-27",
             "date_stop": "2022-06-03"
          },

Here is the code I have used but it is wrong.

 result = arr.reduce((r,o) => {r[o.action_type] = o.value; return r; }, {});

Output should be like the following way

 "data": [
      {
         "campaign_name": "Daily Shopping - [Reach Ad]",
         "reach": "2365439",
         "clicks": "43692",
         "impressions": "4873908",
         "like": "1",
         "comment: "40"
       }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This can be achieved with Object.fromEntries and spread syntax in object literals:

const arr = [{"campaign_name": "Daily Shopping - [Reach Ad]","reach": "2365439","clicks": "43692","impressions": "4873908","actions": [{"action_type": "like","value": "1"},{"action_type": "comment","value": "40"}],"date_start": "2022-05-27","date_stop": "2022-06-03"},];
          
const result = arr.map(({actions, ...rest}) => ({
    ...rest,
    ...Object.fromEntries(actions.map(({action_type, value}) => 
        [action_type, value]
    ))
}));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Below is an implementation using Object.entries() and reduce()

const raw_data = `{
  "data": [
      {
         "campaign_name": "Daily Shopping - [Reach Ad]",
         "reach": "2365439",
         "clicks": "43692",
         "impressions": "4873908",
         "actions": [
            {
               "action_type": "like",
               "value": "1"
            },
            {
               "action_type": "comment",
               "value": "40"
             }
         ],
         "date_start": "2022-05-27",
         "date_stop": "2022-06-03"
      }
    ]
   }`;

const data = JSON.parse(raw_data).data;

const processedData = Object.entries(data[0]).reduce(
  (accumulator, [currentProperty, currentValue]) => {
    if (currentProperty !== "actions") {
      if (!currentProperty.includes("date"))
        accumulator[currentProperty] = currentValue;
    } else {
      currentValue.forEach(
        (action) => (accumulator[action.action_type] = action.value)
      );
    }
    return accumulator;
  },
  {}
);

console.log(processedData);
about 4 years ago · Juan Pablo Isaza Report

0

Your code is almost correct. As you can see, only the initial argument passed to the reducer needs to be modified. The modification can be applied in situ.

Code

 let s_x = `{
      "data": [
          {
             "campaign_name": "Daily Shopping - [Reach Ad]",
             "reach": "2365439",
             "clicks": "43692",
             "impressions": "4873908",
             "actions": [
                {
                   "action_type": "like",
                   "value": "1"
                },
                {
                   "action_type": "comment",
                   "value": "40"
                 }
             ],
             "date_start": "2022-05-27",
             "date_stop": "2022-06-03"
          }
        ]
       }`
   , x = JSON.parse(s_x)
   ;


x.data.forEach ( po_orig => {
    let o_transformed = po_orig.actions.reduce ((r,o) => {
            r[o.action_type] = o.value; return r;
        }, po_orig) // Note: po_orig instead of {}
      ;

    delete po_orig['actions'];
}); 

console.log(`new data: `, x);

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!