Create new array with some object properties

Let's say you have an array of objects like this:

const originalArray = [
  { id: 1, name: 'John', age: 25, occupation: 'Developer' },
  { id: 2, name: 'Jane', age: 30, occupation: 'Designer' },
  { id: 3, name: 'Bob', age: 35, occupation: 'Manager' }
];

And you want to create a new array with only certain properties from each object. For example, you might want to create a new array with only the name and age properties from each object.

You can use the map() method to achieve this:

const newArray = originalArray.map((obj) => ({ name: obj.name, age: obj.age }));

This will create a new array with objects that have only the name and age properties:

const newArray = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 30 },
  { name: 'Bob', age: 35 }
];

Alternatively, you can use the reduce() method to achieve the same result:

const newArray = originalArray.reduce((acc, obj) => {
  acc.push({ name: obj.name, age: obj.age });
  return acc;
}, []);

This will also create a new array with objects that have only the name and age properties.

You can also use the Object.assign() method to create a new object with only certain properties, and then add it to the new array:

const newArray = originalArray.map((obj) => Object.assign({}, obj, { occupation: undefined }));

This will create a new array with objects that have only the name and age properties, and the occupation property will be undefined.

Note that in all of these examples, we're using the map() method to create a new array with the desired properties. If you want to create a new array with a different structure, you may need to use a different method, such as reduce() or filter().