addkey
v0.9.1
Published
Add a new key-value pair for a single item of array type and filter the children
Downloads
2
Maintainers
Readme
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:
$ npm install --save addkey
Usage
Works with arrays.
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
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, xiaoyueyue. Released under the MIT License.