Releasing V1.0.0 of move-position: Utility Functions for moving index in an array

栏目: IT技术 · 发布时间: 6年前

内容简介:Move element in given array form index-A to index-BThis project is licensed under the

move-position

Move element in given array form index-A to index-B :scissors:

npm install move-position

move

/**
 * Moves element form/to index.
 *
 * @param {Array} [arr=[]]
 * @param {number} from
 * @param {number} to
 * @param {boolean} [isMutate=true]
 * @returns {Array}
 */
const modifiedArr = move(arr, from, to, isMutate);

Example(1)

const input = ["a", "b", "c"];

// move element form index=0, to index=2
const result = move(input, 0, 2);

// ["b", "c", "a"];

moveMultiArr

/**
 * Moves the same index in multiple arrays
 *
 * @param {Array} [arr=[]] Array contain arrays to be changed
 * @param {number} from - targeted index
 * @param {number} to - targeted index
 * @param {boolean} [isMutate=true]
 * @returns {Array}
 */
const modifiedArr = moveMultiArr([arr1, arr2, ...], from, to, isMutate);

Example(2)

const input1 = ["a1", "b1", "c1"];
const input2 = ["a2", "b2", "c2"];

const inputs = [input1, input2];

const result = moveMultiArr(inputs, 2, 0);

// result[0] > ["c1", "a1", "b1"];
// result[1] > ["c2", "a2", "b2"];

moveMultiIndex

/**
 * Moves multiple indexes in the same array
 *
 * @param {Array} [arr=[]]
 * @param {Object[]} movingMap
 * @returns {Array} new Array with index changes
 */
const modifiedArr = moveMultiIndex(arr, [{from, to}, ...]);

Example(3)

const input = ["a", "b", "c"];

const movingMap = [
  { from: 0, to: 2 },
  { from: 2, to: 1 }
];

const result = moveMultiIndex(input, movingMap);

// result > [ 'a', 'c', 'a' ]

Related projects

Tests

npm test

License

This project is licensed under the GPL-3.0 License


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google御用網頁語言Node.js

Google御用網頁語言Node.js

郭家寶 / 佳魁資訊 / 2013-4-26 / NT 490

這是一本 Node.js 的入門教學,寫給想要學習 Node.js,但沒有任何系統的經驗的開發者。如果你聽說過 Node.js,並被它許多神奇的特性吸引,本書就是為你準備的。 透過閱讀本書,你可以對 Node.js 有全面的認識,學會如何用 Node.js 程式設計,了解事件驅動、非同步式 I/O 的程式設計模式,同時還可以了解一些使用JavaScript 進行函數式程式設計的方法。 ......一起来看看 《Google御用網頁語言Node.js》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具