historyReverser & array reverse
"use strict";/**** @author xgqfrms* @license MIT* @copyright xgqfrms** @description historyReverser* @augments Reverse 逆向 / Recursive 递归* @example* @link**/const historyReverser = (datas = [], text = ``, debug = false) => {let result = {};let newDatas = [];let reverseDatas = [];let length = datas.length;if (length === 10) {if(datas.includes(text)) {// remove old & add newlet index = datas.indexOf(text);datas.splice(index, 1);datas.push(text);} else {// add newdatas.push(text);datas.shift(0);}} else {datas.push(text);}newDatas = datas;datas.forEach((item, i) => {let value = datas[length - i - 1];reverseDatas.push(value);});result = {newDatas,reverseDatas,};return result;
};export default historyReverser;export {historyReverser,
};
tesing
let datas = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]let text = "5";
// let text = "15";
const historyReverser = (datas = [], text = ``, debug = false) => {let result = {};let newDatas = [];let reverseDatas = [];let length = datas.length;if (length === 10) {if(datas.includes(text)) {// remove old & add newlet index = datas.indexOf(text);datas.splice(index, 1);datas.push(text);} else {// add newdatas.push(text);datas.shift(0);}} else {datas.push(text);}newDatas = datas;datas.forEach((item, i) => {let value = datas[length - i - 1];reverseDatas.push(value);});result = {newDatas,reverseDatas,};return result;
};let ReverseArray = historyReverser(datas, text);console.log(`ReverseArray =`, ReverseArray);
MDN & Array
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
0.push
- shift
unshift
- slice
splice
includes
6.infdexOf
7.lastIndexOf
bug
"use strict";/**** @author xgqfrms* @license MIT* @copyright xgqfrms** @description historyReverser* @augments Reverse 逆向 / Recursive 递归* @example* @link**/const historyReverser = (datas = [], text = ``, debug = false) => {if (text === null) {text = `xyz`;}let result = {};let newDatas = [];let reverseDatas = [];let length = datas.length;if (length === 10) {if(datas.includes(text)) {// remove old & add newlet index = datas.indexOf(text);datas.splice(index, 1);datas.push(text);} else {// add newdatas.push(text);datas.shift(0);}if(datas.includes(text)) {// remove old & add newlet index = datas.indexOf(text);datas.splice(index, 1);datas.push(text);} else {// add newdatas.push(text);datas.shift(0);}} else {datas.push(text);}newDatas = datas;// datas.forEach(// (item, i) => {// let value = datas[length - i - 1];// console.log(`null value`, value);// reverseDatas.push(value);// }// );if (1 <= length) {datas.forEach((item, i) => {let value = datas[length - i - 1];console.log(`null value`, value);reverseDatas.push(value);});} else {reverseDatas = datas;}result = {newDatas,reverseDatas,};return result;
};export default historyReverser;export {historyReverser,
};
OK
unshfit
"use strict";/**** @author xgqfrms* @license MIT* @copyright xgqfrms** @description historyReverser* @augments Reverse 逆向 / Recursive 递归* @example* @link**/const historyReverser = (datas = [], text = ``, debug = false) => {// console.log(`datas =`, datas);// console.log(`text =`, text);let result = {};let newDatas = [];let reverseDatas = [];let length = datas.length;// if(datas.indexOf(text) !== -1) {if(datas.includes(text)) {// remove old & add newlet index = datas.indexOf(text);datas.splice(index, 1);console.log(`datas =`, datas);datas.unshift(text);// datas.push(text);} else {// add newif (length === 10) {datas.splice(9, 1);datas.unshift(text);// datas.push(text);// datas.shift(0);} else {// add newdatas.unshift(text);// datas.push(text);}}newDatas = datas;reverseDatas = datas;// let new_length = newDatas.length;// if (1 <= new_length) {// let temp = datas;// reverseDatas = temp.reverse();// // datas.forEach(// // (item, i) => {// // let value = datas[new_length - i - 1];// // // console.log(`value`, value);// // reverseDatas.push(value);// // }// // );// } else {// reverseDatas = datas;// }result = {newDatas,reverseDatas,};return result;
};export default historyReverser;export {historyReverser,
};