技術情報
- 2019年12月12日
- 技術情報
CakePHP Windows10環境構築ガイド
nishida at 2019年12月12日 10:00:54
- 2019年12月06日
- 技術情報
Lodash Js Array & Object – Part 3 (Final)
Today I will talk about dealing with array and object in Lodash Js.
Filter
- Filtering a list to transform the elements we want to get back
1 2 3 4 5 |
var numbers = _.range(0,10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] var evenNumbers = _.filter(numbers, function(e){ return e % 2 != 0; }); // evenNumbers is now [ 0, 2, 4, 6, 8 ] |
Reduce
- Reducing a list of objects to a single value.
- Ex=>A group of people can buy a meal or not. In that case we have to check out they have the enough amount by the sum of money they have.
- (Each’s money => sum money)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var ppl = [ { 'name': 'Keita', 'money': 1000 }, { 'name': 'Sato', 'money': 3000 }, { 'name': 'Ruma', 'money': 500 }, ] var sumMoney = function(data){ return _.reduce( data, function(accumulated, e){ return accumulated + e.money; }, 0 ); } function canBuyMeal(data){ return 3500 < sumMoney(data); } canBuyMeal(ppl); // returns true |
Some
- If there is at least one record in a collection that meets some criteria.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var ppl = [ { 'name': 'Keita', 'hasMoney': true }, { 'name': 'Sato', 'hasMoney': false }, { 'name': 'Ruma', 'hasProperty': true }, ] function canLive(data){ return _.some(data, function(e){ return e.hasProperty; }); } canGroupDrive(ppl); // returns true |
Map
- Useful for changing a list into a different list in a purely declarative way.
- Can just specify how you want to manipulate an element of a list.
- Make a new list transformed by providing a function
1 2 3 4 5 |
var res1 = _.range(5); // [ 0, 1, 2, 3, 4] var res2 = _.map(a, function(e){ return e + e;} ); //result is [0, 2, 4, 6, 8] |
By Yuuma
yuuma at 2019年12月06日 11:00:39
- 2019年12月05日
- 技術情報
[php] printf sprintf vprintf vsprintf の違いについて(その2)
nishida at 2019年12月05日 10:00:45
- 2019年11月29日
- 技術情報
Lodash Js Part 2(Chaining and has method)
Today I will talk about chaining functions and has method.
Chaining
Without Chaining
1 2 3 4 5 6 7 8 9 10 11 |
var data1 = [10, 15, 20, 25, 15]; var data2 = _.filter(data1, function(item){ return item % 10 === 5 }); // data2 now contains [15, 25, 15] var data3 = _.uniq(data2); // data3 now contains [15, 25] var data4 = _.map(data3, function(item){ return item + 1 }); // data4 now contains [16, 26] |
With Chaining
1 2 3 4 5 6 7 8 9 |
var data1 = [10, 15, 20, 25, 15]; var data4 = _(data1) .filter(function(item){ return item % 10 === 5 }) .uniq() .map(function(item){ return item + 1 }) .value(); // data4 now contains [16, 26] |
- The chaining is more efficient.
- no intermediate results are created.
Slight Changes for Explicit Chain
& Implicit Chain
Explicit chaining
Still need to unwrap the result with .value().
1 2 3 4 5 6 7 8 |
var data = [1, 5, 10, 15, 5, 10, 1, 20]; var unqSum = _.chain(data) .uniq() .sum() .value(); result is 51 |
Implicit chaining
The “unwrapping” of the single value is implied. (there is no need to call .value().)
1 2 3 4 5 6 7 |
var data = [1, 5, 10, 15, 5, 10, 1, 20]; var unqSum = _(data) .uniq() .sum(); //result is 51 |
has
- Determine if an object has (or contains) a key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var obj = { data1: 2, data2: 3, data3: { data3_1:40, data3_2:{ data3_2_1:500 } } }; var res1 = _.has(obj, "data1"); // true var res2 = _.has(obj, "data1.data2"); // false var res3 = _.has(obj, "data3"); // true var res4 = _.has(obj, "data3.data3_2"); // true var res5 = _.has(obj, "data3.res3_2_1"); // false var res6 = _.has(obj, "data3.data3_1.data3_2_1"); // false var res7 = _.has(obj, "data3.data3_2.data3_2_1"); // true |
- Arrays can be used to split up parts.
1 2 |
var res8 = _.has(obj, ["data1", "data2"]); // false, same as res2 var res9 = _.has(obj, ["data3", "data3_2"]); // true, same as res4 |
By Yuuma
yuuma at 2019年11月29日 11:00:07
- 2019年11月28日
- 技術情報
[php] printf sprintf vprintf vsprintf の違いについて(その1)
nishida at 2019年11月28日 10:00:45