« Lodash » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 7 : Ligne 7 :
<kode lang='js'>
<kode lang='js'>
// for string, it checks for a substring of value
// for string, it checks for a substring of value
_.includes('abcd', 'bc');
if (_.includes('abcd', 'bc')) { }
// => true
// true
</kode>
</kode>



Version du 28 janvier 2021 à 17:07

Links

includes

Checks if value is in collection.

Js.svg
// for string, it checks for a substring of value
if (_.includes('abcd', 'bc')) { }
// true

replace

Js.svg
const result = _.replace('A.B.C', '.', '_');
// A_B_C

forOwn

Iterates over own enumerable string keyed properties of an object.

Js.svg
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