title: Return subset of JSON Object using Javascript map() function
body: <p>My question is if there is a simple way to return a subset of JSON object that will contain all 'columns' rather than specifying individually which 'columns' to return. </p>
  
  <p>In particular, I have a multi-column csv file converted to JSON. The function below returns a subset of that object - just two 'columns' (as another object) if certain condition is met (in this case 'Name' is matched):</p>
  
  <pre><code>var firstSubArr = json.map(function(s) {
    if (s.Name === RegName) {
      return {
        'Price': s.Price,
        'Name': s.Name
      }
    }
  }).filter(function(n) {
    return n !== undefined
  });
  </code></pre>
  
  <p>Is there a simpler way to return "all columns" from json object as object rather that this: </p>
  
  <pre><code>return {'Price':s.Price,'Name':s.Name}?
  </code></pre>
  
  <p>Comment:
  It is just a simple JSON structure after conversion from csv, eg:</p>
  
  <pre>
  `[
      {Name:'Sydney', Price:123, Type:'xyz', etc. 20 more... },
      etc.
      ]`
  </pre>
  
  <p>Comment 2: Yes filter may be an option as a couple of people suggested but what if my condition is a bit more complex, like:</p>
  
  <pre>
      var fullPeriodArr= json.map( function (s) { 
      if(moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid || s.Type==sid2)&& s.Name===RegName){return [s.Price, s.Name] 
      }
      }).filter( function (n) { 
      return n!== undefined
      });
  </pre>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>All 3 respondents provided the clue, thanks! It was just “return every object in the array of objects that matches the condition”, or simply: </p>
  
  <pre>
      var firstSubArr= json.filter( 
  
  s =>  (moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid  || s.Type==sid2) && s.Name===RegName)
  
  );
  </pre>
  
  <p>. where time range 'from... to' is evaluated using moment.js lib and Date, Type and Name are object keys. </p>
  
  <p>The secret pudding was the round bracket ( ) around the complex condition. </p>
  
  <p>How neat Javascript has become: no need to get .length nor to loop with 'for' or 'while', no 'if... else' statements, or no need to store interim results, and no 'return'. The arrow replaces all that!</p>
  
  <p>Then you can access eg. Price 'column' as array of numbers for summary calculations (eg. sum of values):</p>
  
  <pre>
  
  var firstSubArrPrice=firstSubArr.map(t => t.Price );
  
  </pre>