Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dynamic-assignment

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynamic-assignment

n级对象动态赋值

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

dynamic-assignment

n级对象动态赋值

how to use

step 1

  • npm install -g cnpm --registry=https://registry.npm.taobao.org
  • cnpm install dynamic-assignment --save
  • 或者使用 yarn add dynamic-assignment

step 2

最好在全局引用 import dynamicAssignment from 'dynamic-assignment';

  • use
var arr = ['a', 'b', 'c'];
var obj = { 
    a: { 
        b: {
            c: 1 
        }, 
        n: 'ha' 
    }, 
    d: [], 
    e: 'la' 
};

适用情况
当需要对obj的子集c赋值的时候, 我们一般直接obj.a.b.c = 'val'; 当我们需要动态处理一些赋值时, 这种方法就太麻烦了,如arr代表对象的层级关系。 在不知道里面内容的情况下,你是不是会根据arr的长度来赋值, 像这样:obj[arr[0]][arr[1]][arr[2]] = 'val'; 显然,每次arr长度不一样的时候,这样处理还是会很麻烦, 这时候只需要做个简单的递归就可以解决问题了。 代码很简单,有兴趣可以自行查看。
示例
dynamicAssignment(obj, arr, 3); console.log('obj', obj); { a: { b: { c: 3 }, n: 'ha' }, d: [], e: 'la' } dynamicAssignment(obj, ['a', 'n'], 'new'); { a: { b: { c: 3 }, n: 'new' }, d: [], e: 'la' } 解决重复劳动力是一件很有意义的事情, 有什么好的想法,欢迎一起成长交流 WeChat:mrliaojun

github

Jared

Keywords

FAQs

Package last updated on 09 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc