Skip to content

Latest commit

 

History

History
92 lines (73 loc) · 1.87 KB

README.md

File metadata and controls

92 lines (73 loc) · 1.87 KB

addKey NPM version NPM monthly downloads NPM total downloads Build Status

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, yanyue404. Released under the MIT License.