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

154
Views
create array on basis of object's child

I am have array like:

[
  {
    "_id": "62434e4c52a870a3840f40fd",
    "Nome": [
      {
        "_id": "61f76c3c7a308dd136f1d33a",
        "value": "Oo",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "Peso": [
      {
        "_id": "61f92ec87a308dd136c0e182",
        "value": "554",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "createdAt": [
      {
        "value": "29/03/2022 03:22:02"
      }
    ]
  },
  {
    "_id": "62449de6ed196e40cb4309ae",
    "Nome": [
      {
        "_id": "61f76c3c7a308dd136f1d33a",
        "value": "Ssg",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "Peso": [
      {
        "_id": "61f92ec87a308dd136c0e182",
        "value": "255",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "createdAt": [
      {
        "value": "29/03/2022 03:23:26"
      }
    ]
  },
],

What I'm having trouble with is loop between each object example Nome, Peso the names of these arrays are dynamic and do not have a static name!

Example of what I'm thinking:

[
  {
    "_id": "62434e4c52a870a3840f40fd",
    "Nome": "Oo",
    "Peso": "554",
    "createdAt": "29/03/2022 03:22:02"
  },
  {
    "_id": "62449de6ed196e40cb4309ae",
    "Nome": "Ssg",
    "Peso": "255",
    "createdAt": "29/03/2022 03:23:26"
  }
],

Thanks for help :D

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

If each Nome, Peso, createdAt property array always has one element, then you can use Array#map as follow:

const input = [ { "_id": "62434e4c52a870a3840f40fd", "Nome": [ { "_id": "61f76c3c7a308dd136f1d33a", "value": "Oo", "updatedAt": "2022-05-04T00:00:00.000Z" } ], "Peso": [ { "_id": "61f92ec87a308dd136c0e182", "value": "554", "updatedAt": "2022-05-04T00:00:00.000Z" } ], "createdAt": [ { "value": "29/03/2022 03:22:02" } ] }, { "_id": "62449de6ed196e40cb4309ae", "Nome": [ { "_id": "61f76c3c7a308dd136f1d33a", "value": "Ssg", "updatedAt": "2022-05-04T00:00:00.000Z" } ], "Peso": [ { "_id": "61f92ec87a308dd136c0e182", "value": "255", "updatedAt": "2022-05-04T00:00:00.000Z" } ], "createdAt": [ { "value": "29/03/2022 03:23:26" } ] } ];

const output = input.map(({_id,...rest}) => ({
    _id,
    ...Object.fromEntries( 
        Object.entries(rest).map(([k,v]) => [k,v[0].value])
    )
}));

console.log( output );

about 4 years ago · Santiago Gelvez Report

0

If considering those dynamic keys will have array then try below code.

const data = [{
    "_id": "62434e4c52a870a3840f40fd",
    "Nome": [{
      "_id": "61f76c3c7a308dd136f1d33a",
      "value": "Oo",
      "updatedAt": "2022-05-04T00:00:00.000Z"
    }],
    "Peso": [{
      "_id": "61f92ec87a308dd136c0e182",
      "value": "554",
      "updatedAt": "2022-05-04T00:00:00.000Z"
    }],
    "createdAt": [{
      "value": "29/03/2022 03:22:02"
    }]
  },
  {
    "_id": "62449de6ed196e40cb4309ae",
    "Nome": [{
      "_id": "61f76c3c7a308dd136f1d33a",
      "value": "Ssg",
      "updatedAt": "2022-05-04T00:00:00.000Z"
    }],
    "Peso": [{
      "_id": "61f92ec87a308dd136c0e182",
      "value": "255",
      "updatedAt": "2022-05-04T00:00:00.000Z"
    }],
    "createdAt": [{
      "value": "29/03/2022 03:23:26"
    }]
  },
];

data.map(d => {
  Object.keys(d).forEach(k => {
    if (Array.isArray(d[k]) && d[k].length > 0)
      d[k] = d[k][0].value;
  });
});

console.log(data);

about 4 years ago · Santiago Gelvez Report

0

The below may be one possible solution to achieve the desired objective.

Code Snippet

const transformArr = arr => (
  arr.map(obj => ({
    ...Object.fromEntries(
      Object.entries(obj)
      .map(([k, v]) => {
        if (v && v.length > 0 &&
            Array.isArray(v) &&
            "value" in v[0]
          ) {
          return [k, v[0].value]
        } else {
          return [k, v]
        }
      })
    )
  }))
);

const origArr = [
  {
    "_id": "62434e4c52a870a3840f40fd",
    "Nome": [
      {
        "_id": "61f76c3c7a308dd136f1d33a",
        "value": "Oo",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "Peso": [
      {
        "_id": "61f92ec87a308dd136c0e182",
        "value": "554",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "createdAt": [
      {
        "value": "29/03/2022 03:22:02"
      }
    ]
  },
  {
    "_id": "62449de6ed196e40cb4309ae",
    "Nome": [
      {
        "_id": "61f76c3c7a308dd136f1d33a",
        "value": "Ssg",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "Peso": [
      {
        "_id": "61f92ec87a308dd136c0e182",
        "value": "255",
        "updatedAt": "2022-05-04T00:00:00.000Z"
      }
    ],
    "createdAt": [
      {
        "value": "29/03/2022 03:23:26"
      }
    ]
  },
];

console.log(transformArr(origArr));

Explanation

  • Iterate over the array
  • Use Object.entries() and Object.fromEntries() to extract key-value [k, v]pairs (as arrays) and then re-create (key-value pairs array back into an) object
  • Check if v is an array and that it has an element at index-0 with a prop value
  • If yes, then transform it to show only the value (of index-0 element)
  • Otherwise, just return v as-is

Constraint

If the inner-array (for "Nome", "Peso" or other "dynamic keys") has more than one element only the value from index-0 element will be considered.

NOTE

This solution does not mutate the original-array. It creates a new copy & if needed this may be re-assigned to the original-array.

about 4 years ago · Santiago Gelvez 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!