Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pluck javascript

function pluck(arr, key) {
  return arr.map(obj => obj[key]);
}
Comment

javascript pluck

// Write a function that returns an array of only the objects with truthy values
// for the passed in key.
//
// pluckTruthy({a: 1, b: '', c: false}, 'a') => [1]
// pluckTruthy({a: 1, b: '', c: false}, 'b') => []
// pluckTruthy({a: 1, b: '', c: false}, 'c') => []
// pluckTruthy({a: 1, b: '', c: false}, 'd') => []
//
function pluckTruthy(obj, key) {
  var result = [];
  for (var i = 0; i < obj.length; i++) {
    if (obj[i][key]) {
      result.push(obj[i][key]);
    }
  }
  return result;
}

// console.log(pluckTruthy({a: 1, b: '', c: false}, 'a'));
Comment

javascript pluck

Example 1:

 <script type="text/javascript">
          var list = [{name: 'jack', age: 14}, {name: 'jill', age: 15},
                             {name: 'humpty', age: 16}];
          console.log(_.pluck(list, 'age'));
    </script>
    

Example 2:

 <script type="text/javascript">
         var goal  = [
        {
            "category" : "other",
            "title" : "harry University",
            "value" : 50000,
            "id":"1"
        },
        {
            "category" : "traveling",
            "title" : "tommy University",
            "value" : 50000,
            "id":"2"
        },
        {
            "category" : "education",
            "title" : "jerry University",
            "value" : 50000,
            "id":"3"
        },
        {    
            "category" : "business",
            "title" : "Charlie University",
            "value" : 50000,
            "id":"4"
        }
    ]
    console.log(_.pluck(goal, 'category'));
    </script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: async fetch api call 
Javascript :: @viewchild elementref 
Javascript :: big numbers factorial js 
Javascript :: how to clean react native project 
Javascript :: calculate time difference in hrs moment 
Javascript :: .style.display 
Javascript :: react proptypes reuse shape 
Javascript :: jsx if block 
Javascript :: sequelize logging insert 
Javascript :: Selector All Element querySelector Method 
Javascript :: get day name from date javascript 
Javascript :: jquery toggle checkbox 
Javascript :: jquery find child of parent sibling 
Javascript :: python get json content from file 
Javascript :: js replace all spaces 
Javascript :: how to pass token in laravel in jquery ajax 
Javascript :: how to remove a part of a string javascript 
Javascript :: array sort by alphabetical javascript 
Javascript :: redirect to another page javascript 
Javascript :: redirect to different page in javascript 
Javascript :: saving json file python 
Javascript :: cypress support ability to set viewport in `before` 
Javascript :: console log state object redux 
Javascript :: inner content 
Javascript :: check overflow react 
Javascript :: get DOM node with xpath 
Javascript :: get column from 2D array javascript 
Javascript :: js stop redirect 
Javascript :: javascript sort object by key 
Javascript :: jquery empty and append 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =