Home > Back-end >  create array on basis of object's child
create array on basis of object's child

Time:04-13

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

CodePudding user response:

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 );

CodePudding user response:

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);

CodePudding user response:

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.

  • Related