My object array is as follows:
{
user_id: '2',
imagefilename: '/data/data',
coordinates: [ { classes_id: 2, coordinate: [Array] } ]
}
array I want to create
{
user_id: '2',
imagefilename: '/data/data'
}
how can I do it.
You could use destructuring:
const A = { user_id: '2', imagefilename: '/data/data', coordinates: [ { classes_id: 2, coordinate: [Array] } ] }
const { coordinates, ...B } = A;
console.log(B);
this way, any A's properties that are specified will be omitted, and the rest of the properties will be added to B. For example if you want to exclude imagefilename field as well, you could use const { coordinates, imagefilename, ...B } = A;
let arr = [{ user_id: '2', imagefilename: '/data/data', coordinates: [ { classes_id: 2, coordinate: [Array] } ] }];
let output = arr.map(a => { return {user_id: a.user_id, imagefilename: a.imagefilename}})
console.log(output);
You could do this using the map function to solve this.