@armelwanes/magick-array
v0.0.4
Published
This is an utilities for array manipulation in javascript
Downloads
3
Readme
magick-array
This is an utilities for array manipulation in javascript
Table of Contents
Install
You can install it by running:
# If you use npm:
npm install @armelwanes/magick-array
# Or if you use Yarn:
yarn add @armelwanes/magick-array
Usage
sum
import {sum} from '@armelgeek/magick-array';
.....
const total = sum([1, 2, 3]);
// 6
copy
import {copy} from '@armelgeek/magick-array';
.....
let original = [1, 2, 3];
const copied = copy(original);
// copied: [1, 2, 3]
group
- test data
const data = [
{
id: 1,
name: "Montre luxe",
priceRegular: 50000,
shop: {
id: 3,
name: "HJ Store",
},
},
{
id: 2,
name: "women's tee-shirt 50/50",
priceRegular: 30000,
shop: {
id: 2,
name: "Daily Market one",
},
},
];
simple:
import {group} from '@armelgeek/magick-array';
.....
const groupedData = group(data,"priceRegular");
/*
Return:
{
"30000": [
{
id: 2,
name: "women's tee-shirt 50/50",
priceRegular: 30000,
shop: { id: 2, name: "Daily Market one" },
},
],
"50000": [
{
id: 1,
name: "Montre luxe",
priceRegular: 50000,
shop: { id: 3, name: "HJ Store" },
},
],
}**/
advance :
import {group} from '@armelgeek/magick-array';
.....
const groupedData = group(data,"shop","name");
/*
Return:
{
"Daily Market one": [
{
id: 2,
name: "women's tee-shirt 50/50",
priceRegular: 30000,
shop: { id: 2, name: "Daily Market one" },
},
],
"HJ Store": [
{
id: 1,
name: "Montre luxe",
priceRegular: 50000,
shop: { id: 3, name: "HJ Store" },
},
],
}**/
cycle
import {Cycle} from '@armelgeek/magick-array';
.....
const cycled = new Cycle([
{ id: 1, name: "armel" },
{ id: 2, name: "liva" },
{ id: 3, name: "aina" },
{ id: 4, name: "santatra" },
]);
cycled.current();
//=> { id: 1, name: "armel" }
cycled.next();
//=> { id: 2, name: "liva" }
cycled.next();
//=> { id: 3, name: "aina" }
cycled.next();
//=> { id: 4, name: "santatra" }
cycled.next();
//=> { id: 1, name: "armel" }
cycled.previous();
//=> { id: 4, name: "santatra" }
const numberCycle = new Cycled([
{ id: 1, name: "armel" },
{ id: 2, name: "liva" },
{ id: 3, name: "aina" },
{ id: 4, name: "santatra" },
]);
console.log(...numberCycle);
flatten
concatenate any array in one array
import {flatten} from '@armelgeek/magick-array';
.....
var data = [
{
id: 1,
contenu: [
{ id: 14, qqt: 20 },
{ id: 12, qtt: 10 },
],
},
{ id: 2, contenu: [{ id: 12, qqt: 2 }] },
];
const b = flatten(data,'contenu');
// [{ id: 14, qqt: 20 },{ id: 12, qtt: 10 },{ id: 12, qqt: 2 }]
Maintainer
Enjoy!
License
MIT © 2022 Armel wanes