The usage of _.set in Lodash

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.