1 _.set (object, path, value)2 # ? ? ?3 4 sets the property value on the path of the object. If the path does not exist, create it.5 6 Parameters7 1. Object: objects to be enlarged.8 2. Path (array|string): Property path.9 3, Value (*): The value that needs to be set.Ten return value One (object): Returns an object. A - Example - var object = {' A ': [{' B ': {' C ': 3}}]}; the - _.set (object, ' A[0].B.C ', 4); - Console.log (OBJECT.A[0].B.C); - //= 4 + - _.set (object, ' x[0].y.z ', 5); + Console.log (object.x[0].y.z); A= 5
The above is a description of the document
Combined with _.each () for more efficient use;
The code is as follows:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "UTF-8">5 <title>HTML5 Canvas Drawing text text Getting started example6 </title>7 </Head>8 <Body>9 <Scriptsrc= "Lodash.js"></Script>Ten <Script> One varb= [ A {name:"222", Age: A}, - {name:"Sdfs22", Age:2}, - {name:"24322", Age: A}, the {name:"22fds", Age: the} - ]; - varSort= {}; - + _.each (b,function(item,i) { - varDis= 1; + _.set (Sort,b[i].name,dis) A }); at Console.log (sort); - </Script> - </Body> - </HTML>
The final result returns an object;
The usage of _.set in Lodash