npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-action

v1.0.3

Published

实现数据的深拷贝(数组对象中的label和key必须要存在)

Downloads

2

Readme

功能描述

实现数据的深拷贝(数组对象中的label和key必须要存在)

  1. 根据数组中的key进行批量赋予数据(数组对象的value上)
  2. 获取数组中所有的value
  3. 根据key值进行key值对应的item进行整体覆盖赋值
  4. 根据key值获取key值对应的value的值
  5. 设置根据key获取key对应的value

安装

npm install array-action

使用

以下方数组为例(如无特殊描述,均采用改示例的数组)

[{label: "测试1",key: "test1"},{label: '测试字段2',key: 'test2'}]
import ArrayAction from 'array-action'
const cloneOption = new ArrayAction([
	{label: "测试1",key: "test1"},
	{label: '测试字段2',key: 'test2'}
]);

方法 (clone之后的) method

除get开头方法外均支持链式调用包括clone

setKeyValue: 实现对某个key值的value进行覆盖更新

let arr = cloneOption.clone().setKeyValue("test1", "test12");
console.log(JSON.stringify(arr)) 
// [{"label":"测试1","key":"test1","value":"test12"},{"label":"测试字段2","key":"test2"}]

getKeyValue: 获取数据中某个key值的value值

let arr = cloneOption.clone()
arr.setKeyValue("test1", "我是测试的value");
arr.getKeyValue("test1")  // 我是测试的value

setArrValue: 将对象赋值给clone后的对象

let arr = cloneOption.clone()
arr.setArrValue({
	test1: '我是test1的value',
	test2: '我是test2的value'
})
console.log(JSON.stringify(arr)) 
// [{"label":"测试1","key":"test1","value":"我是test1的value"},{"label":"测试字段2","key":"test2","value":"我是test2的value"}]

getArrValue 与setArrValue对应

let arr = cloneOption.clone()
arr.setArrValue({
	test1: "我是test1的value",
	test2: "我是test2的value"
})
console.log(arr.getArrValue()) // {test1: '我是test1的value', test2: '我是test2的value'}

setKeyItem: 根据key覆盖更新该key对应的数据

let arr = cloneOption.clone()
arr.setKeyItem("test1", {
	a: "aaa",
	b: "cccc",
	options: [{ ttt: "ttt" }]
})
console.log(JSON.stringify(arr))
// [{"key":"test1","a":"aaa","b":"cccc","options":[{"ttt":"ttt"}]},{"label":"测试字段2","key":"test2"}]

addKeyItem: 根据key值添加该key对应的数据

let arr = cloneOption.clone()
arr.addKeyItem({
	key: 'test3',
	a: "aaa",
	b: "cccc",
	options: [{ ttt: "ttt" }]
})

getAll: 获取操作之后所有数组对象

let arr = cloneOption.clone()
arr.setKeyItem("test1", {
	a: "aaa",
	b: "cccc",
	options: [{ ttt: "ttt" }]
})
console.log(arr.getAll())
// [
//   {
//     key: "test1",
//     a: "aaa",
//     b: "cccc",
//     options: [
//       {
//         ttt: "ttt"
//       }
//     ]
//   },
//   {
//     label: "测试字段2",
//     key: "test2"
//   }
// ]

changeData: 修改clone的原数据的某个item(根据key)

注意:会影响到后续使用clone方法的内容,建议使用在初始化或者重新进行数据的拷贝时候使用。 场景一:我需要对某个内容进行赋值,比如我这个需要一个optipns的数组数据,以还原选择框时候可以使用changeData,这样只要是使用该数据源的,均可以拿到该item的options,如果是请求获取到的,可以节省数据源的请求次数。

let arr = cloneOption.clone()
cloneOption.changeData("test1", {
	value: 1,
	options: [
		{
			label: "禁用",
			value: 0
		},
		{
			label: "启用",
			value: 1
		}
	]
})
console.log(arr) // 已经clone的数据,不会受到影响
console.log(JSON.stringify(cloneOption.clone().getAll())) // 会影响到后续clone的内容打印如下 [{"key":"test1","value":1,"options":[{"label":"禁用","value":0},{"label":"启用","value":1}]},{"label":"测试字段2","key":"test2"}]