Array of objects to array What would be the "correct" way of transforming an array of objects into an object that contains an array?

Suppose I have this array that contains objects:

const paintings = [
  { painting: "Mona Lisa" },
  { painting: "The Starry Night" },
  { painting: "The Last Supper" },
  { painting: "Girl with a Pearl Earring" },
  { painting: "American Gothic" },
  { painting: "The Night Watch" }
];


And I would like to transform it into this:

const paintings = {
  artworks: [
    "Mona Lisa",
    "The Starry Night",
    "The Last Supper",
    "Girl with a Pearl Earring",
    "American Gothic",
    "The Night Watch"
  ]
};


I know that I can accomplish this with two nested loops like below, but is there a more "elegant" way of doing it? I think that these tasks are nowadays usually solved using map or reduce functions but I find them a bit confusing to use. I would like to learn though!

const paintingsNew = {
  artworks: []
};

for (const painting of paintings) {
  for (const val of Object.values(painting).values()) {
    paintingsNew.artworks.push(val);
  }
}

console.log(paintingsNew);

// { artworks:
//    [ 'Mona Lisa',
//      'The Starry Night',
//      'The Last Supper',
//      'Girl with a Pearl Earring',
//      'American Gothic',
//      'The Night Watch' ] }
Your code flattens the entire array of {} objects, indiscriminately flattening all properties. It does not mind, if there is only one single property, but it does not make sense semantically, especially if the array is named paintings.

Rather consider the resulting array a projection/mapping to a single property painting.

For a consistent array check and take only the painting property.

for (const p of paintings) {
  if ("painting" in p) {
    paintingsNew.artworks.push(p.painting);
  }
}


The extra if protects against undefined entering corrupted data.