Home > Enterprise >  how filtering works on array of objects javascript
how filtering works on array of objects javascript

Time:10-18

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.

CodePudding user response:

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;

CodePudding user response:

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.

  • Related