« Lodash » : différence entre les versions
Apparence
Page créée avec « Category:Javascript = Links = * [https://lodash.com/docs Documentation] » |
|||
(9 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 2 : | Ligne 2 : | ||
= Links = | = Links = | ||
* [https://lodash.com/docs Documentation] | * [https://lodash.com/docs Documentation] | ||
= Collection = | |||
== [https://lodash.com/docs/4.17.15#includes includes] == | |||
Checks if value is in collection. | |||
<kode lang='js'> | |||
// for string, it checks for a substring of value | |||
if (_.includes('abcd', 'bc')) { } | |||
// true | |||
</kode> | |||
= Array = | |||
== [https://lodash.com/docs/4.17.15#uniq uniq] == | |||
Creates a duplicate-free version of an array. | |||
<kode lang='js'> | |||
const duplicateFreeArray = _.uniq([1, 2, 2]); | |||
// [1, 2] | |||
</kode> | |||
= String = | |||
== [https://lodash.com/docs/4.17.15#replace replace] == | |||
Replace pattern match with replacement. | |||
<kode lang='js'> | |||
// only the first match is replaced | |||
const result = _.replace('A.B.C', '.', '_'); | |||
// A_B.C | |||
// with a regex with 'g', all the matches are replaced | |||
const result = _.replace('A.B.C', /\w/g, '_'); | |||
// _._._ | |||
</kode> | |||
= Object = | |||
== [https://lodash.com/docs/4.17.15#forOwn forOwn] == | |||
Iterates over own enumerable string keyed properties of an object. | |||
<kode lang='js'> | |||
let json = { | |||
"A": "1", | |||
"B": "2", | |||
"C": { | |||
"C1": "3", | |||
"C2": "4" | |||
}, | |||
"D": [ "5", "6" ] | |||
}; | |||
_.forOwn(json, (value, key) => { | |||
console.log(`key: ${key} - value: ${value}`); | |||
}); | |||
// key: A - value: 1 | |||
// key: B - value: 2 | |||
// key: C - value: [object Object] | |||
// key: D - value: 5,6 | |||
</kode> |
Dernière version du 29 janvier 2021 à 15:21
Links
Collection
includes
Checks if value is in collection.
// for string, it checks for a substring of value
if (_.includes('abcd', 'bc')) { }
// true
|
Array
uniq
Creates a duplicate-free version of an array.
const duplicateFreeArray = _.uniq([1, 2, 2]);
// [1, 2]
|
String
replace
Replace pattern match with replacement.
// only the first match is replaced
const result = _.replace('A.B.C', '.', '_');
// A_B.C
// with a regex with 'g', all the matches are replaced
const result = _.replace('A.B.C', /\w/g, '_');
// _._._
|
Object
forOwn
Iterates over own enumerable string keyed properties of an object.
let json = {
"A": "1",
"B": "2",
"C": {
"C1": "3",
"C2": "4"
},
"D": [ "5", "6" ]
};
_.forOwn(json, (value, key) => {
console.log(`key: ${key} - value: ${value}`);
});
// key: A - value: 1
// key: B - value: 2
// key: C - value: [object Object]
// key: D - value: 5,6
|