Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yanyue404/addkey
Add a new key-value pair for each single item of the array type and filter the children
https://github.com/yanyue404/addkey
Last synced: 10 days ago
JSON representation
Add a new key-value pair for each single item of the array type and filter the children
- Host: GitHub
- URL: https://github.com/yanyue404/addkey
- Owner: yanyue404
- License: mit
- Created: 2019-04-12T02:08:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-20T11:14:51.000Z (over 4 years ago)
- Last Synced: 2024-08-09T22:12:45.687Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# addKey [![NPM version](https://img.shields.io/npm/v/addkey.svg?style=flat)](https://www.npmjs.com/package/addkey) [![NPM monthly downloads](https://img.shields.io/npm/dm/addkey.svg?style=flat)](https://npmjs.org/package/addkey) [![NPM total downloads](https://img.shields.io/npm/dt/addkey.svg?style=flat)](https://npmjs.org/package/addkey) [![Build Status](https://travis-ci.org/yanyue404/addKey.svg?branch=master)](https://travis-ci.org/yanyue404/addKey)
> Add a new key-value pair for each single item of the array type and filter the children
## Usage
Works with arrays.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save addkey
```## Usage
Works with arrays.
```js
const addKey = require("addkey");
const moment = require("moment");
const toTime = ctime => moment(ctime * 1000).format("YYYY-MM-DD");let arr = [
{
id: 1,
name: "亚瑟",
ctime: "1554863144"
},
{
id: 2,
name: "狄仁杰",
ctime: "1557045386"
},
{
id: 3,
name: "曹操",
ctime: "1548040191"
}
];
addKey(arr, (v, index, array) => {
v.time = toTime(v.ctime);
delete v.ctime;
});// log arr =>
/* [
{ id: 1, name: "亚瑟", time: "2019-04-10" },
{ id: 2, name: "狄仁杰", time: "2019-05-05" },
{ id: 3, name: "铠", time: "2019-01-21" }
]; */
```or
```js
const addKey = require("addkey");let arr = [
{
id: 1,
name: "亚瑟"
},
{
id: 2,
name: "狄仁杰"
},
{
id: 3,
name: "曹操"
}
];
addKey(arr, { isShow: false }, (v, index, array) => {
index === array.length - 1 ? ((v.name = "铠"), (v.isShow = true)) : "";
});// log arr =>
/* [
{ id: 1, name: "亚瑟", isShow: false },
{ id: 2, name: "狄仁杰", isShow: false },
{ id: 3, name: "铠", isShow: true }
]; */
```### License
Copyright © 2019, [yanyue404](https://github.com/yanyue404).
Released under the [MIT License](LICENSE).