0

is there a better way to do this? I want return specific properties from object. This way works but i thinking that is not the best way.

target.formats.map(format => {
  const {
    id,
    format_type_id,
    payment_type,
    initial_date,
    final_date,
    estimated_audience,
    direction_type_id,
    price
  } = format

  return {
    id,
    format_type_id,
    payment_type,
    initial_date,
    final_date,
    estimated_audience,
    direction_type_id,
    price
  }
})
Bruno Quaresma
  • 5,231
  • 4
  • 25
  • 41

4 Answers4

1

Well you can do it in a very straightforward manner without destructuring assignment

target.formats.map(f => ({
  f.id,
  f.format_type_id,
  f.payment_type,
  f.initial_date,
  f.final_date,
  f.estimated_audience,
  f.direction_type_id,
  f.price
})

Or you could write a generic function which picks certain properties

const props = xs => y => {
  let acc = {}
  for (let x of xs) acc[x] = y[x]
  return acc;
}

target.formats.map(props([
  'id',
  'format_type_id',
  'payment_type',
  'initial_date',
  'final_date',
  'estimated_audience',
  'direction_type_id',
  'price'
]))
Thank you
  • 107,507
  • 28
  • 191
  • 224
0

You could use the deconstruction in the parameter list with some extra parentheses.

target.formats.map(({ id, format_type_id /* ... */ }) => ({ id, format_type_id /* ... */ }));
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • But then you have to write the list of properties twice – fafl Jan 04 '17 at 13:28
  • @fafl, yes, obviously. – Nina Scholz Jan 04 '17 at 13:33
  • I don't see how it resolve duplication problem... :/ – Bruno Quaresma Jan 04 '17 at 15:43
  • it does not. it moves the variables to the parameter part. anyway, you need some indicator which parts should be included in the new object. either by iteration and an array, like [Toni's](http://stackoverflow.com/a/41465676/1447675) or [fafl's](http://stackoverflow.com/a/41464829/1447675) answers or with your deconstructuring, or mine *without the need of `format` variable*. – Nina Scholz Jan 04 '17 at 15:49
0

So you want to return only specific attributes from an object? Try this:

var bigObj = {a: 1, b: 2, c: 3, d: 4}

function keepProperties(props, obj) {
  var result = {}
  for (var i = 0; i < props.length; i++) {
    var prop = props[i]
    result[prop] = obj[prop]
  }
  return result;
}

console.log(keepProperties(["a", "c"], bigObj))

So you can run something like

target.formats.map(f => keepProperties(["id", "format_type_id"], f))
fafl
  • 6,094
  • 2
  • 23
  • 40
  • Let's not replace standard [language](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) [features](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) with less powerful custom code. (Also a nice overview of destructuring may be found [here](http://exploringjs.com/es6/ch_destructuring.html)). – user268396 Jan 04 '17 at 13:28
  • I don't think these features are a good fit here – fafl Jan 04 '17 at 14:54
  • @user268396 how destructuring can resolve the problem? – Bruno Quaresma Jan 04 '17 at 15:41
-1

Why not create a method on your object to return it in your format?

//Define object rules
var myClass = (function() {
  function myClass() {
    this.a = 1;
    this.date = new Date();
  }
  myClass.prototype.format = function() {
    return {
      a: this.a,
      d: this.date
    };
  };
  return myClass;
}());
//Create an object
var obj = new myClass();
//Log in format
console.log(obj.format());
Emil S. Jørgensen
  • 5,684
  • 1
  • 9
  • 22
  • Let's not replace standard [language](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) [features](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) with less powerful custom code. (Also a nice overview of destructuring may be found [here](http://exploringjs.com/es6/ch_destructuring.html)). – user268396 Jan 04 '17 at 13:28
  • @user268396 destructuring and spread are good features, but not entirely cross browser compatible so alternatives to language features is relevant. – Emil S. Jørgensen Jan 04 '17 at 13:54
  • @user268396 the object has 15 properties. How this link can resolve the problem? – Bruno Quaresma Jan 04 '17 at 15:43