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 :: js strict mode 
Javascript :: addEventListener call only once 
Javascript :: foreach javascript 
Javascript :: arrondi js 
Javascript :: find one with specofoc id mongoose 
Javascript :: what is the type of children prop 
Javascript :: es6 create array with increasing number 
Javascript :: tsconfig.json not generated 
Javascript :: adding numbers in an array javascript 
Javascript :: how to get specific word from the string javascript 
Javascript :: react useref 
Javascript :: scrollto js 
Javascript :: Javascript get sum of array values 
Javascript :: js remove the last character from a string 
Javascript :: javascript toutcstring 
Javascript :: users api testing 
Javascript :: type of data model mongodb 
Javascript :: toast angular 
Javascript :: html form post json example 
Javascript :: reversed array loop 
Javascript :: get url react 
Javascript :: replace space with hyphen/dash javascript 
Javascript :: javascript howdo i redirect-to another webpage 
Javascript :: jquery boilerplate 
Javascript :: embed example discord.js 
Javascript :: nextjs multer rename file 
Javascript :: stomp.min.js cdn 
Javascript :: dynamic import javascript 
Javascript :: axios network error react native 
Javascript :: how to check if a javascript object is empty 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =