简体   繁体   English

如何转换数组?

[英]How to transform an array?

I have the following task:我有以下任务:

Your task is to implement the function transform(arr) that takes an array and returns transformed array, based on the control sequences that arr contains.你的任务是实现 function transform(arr) ,它接受一个数组并根据arr包含的控制序列返回转换后的数组。 Control sequences are defined string elements of the mentioned array:控制序列是上述数组的定义字符串元素:

  • --discard-next excludes the next element of the array from the transformed array. --discard-next从转换后的数组中排除数组的下一个元素。
  • --discard-prev excludes the previous element of the array from the transformed array. --discard-prev从转换后的数组中排除数组的前一个元素。
  • --double-next doubles the next element of the array in the transformed array. --double-next将转换后的数组中的下一个元素加倍。
  • --double-prev doubles the previous element of the array in the transformed array. --double-prev将转换后的数组中数组的前一个元素加倍。

For example:例如:

transform([1, 2, 3, '--double-next', 4, 5]) => [1, 2, 3, 4, 4, 5]

transform([1, 2, 3, '--discard-prev', 4, 5]) => [1, 2, 4, 5]

The function must not affect initial array. function 不得影响初始阵列。 Control sequences are applied from left to right.控制序列从左到右应用。 Control sequences do not fall into the transformed array.控制序列不属于转换后的数组。 Control sequences in initial array don't occur in a row.初始数组中的控制序列不会连续出现。 If there is no element next to the control sequence to which it can be applied, it does nothing.如果控制序列旁边没有可以应用它的元素,则它什么也不做。 The function should throw an Error if the arr is not an Array .如果arr不是Array , function 应该抛出Error

I've written the following code:我编写了以下代码:

 let transform = function (arr) { for (let i = 0; i < arr.length; i++) { if (arr[i] === '--discard-next') { let result = arr.slice(0, i) + "," + arr.slice(i + 2); let newArray = result.split(','); return newArray; } else if (arr[i] === '--discard-prev') { let result = arr.slice(0, i - 1) + "," + arr.slice(i + 1); let newArray = result.split(','); return newArray; } else if (arr[i] === '--double-next') { let result = arr.slice(0, i) + "," + arr[i + 1] + "," + arr.slice(i + 1); let newArray = result.split(','); return newArray; } else if (arr[i] === '--double-prev') { let result = arr.slice(0, i) + "," + arr[i - 1] + "," + arr.slice(i + 1); let newArray = result.split(','); return newArray; } } }; console.log(transform([1, 2, 3, '--double-next', 4, 5])); // => [1, 2, 3, 4, 4, 5] console.log(transform([1, 2, 3, '--discard-prev', 4, 5])); // => [1, 2, 4, 5]

What's wrong with it?它出什么问题了?

You could flatMap the values and have a look ahead or behind of the wanted transformation.您可以对值进行平面映射,并查看所需转换的前后。

 let transform = array => array.flatMap((v, i, a) => { if (a[i + 1] === '--discard-prev') return []; if (a[i - 1] === '--double-next') return [v, v]; if (a[i + 1] === '--double-prev') return [v, v]; if (v.toString().startsWith('--d')) return []; return v; }); console.log(...transform([1, 2, 3, '--double-next', 4, 5])); // [1, 2, 3, 4, 4, 5] console.log(...transform([1, 2, 3, '--discard-prev', 4, 5])); // [1, 2, 4, 5] console.log(...transform([1, 2, 3, '--double-prev', 4, 5])); // [1, 2, 3, 3, 4, 5]

Use .join(',');使用.join(','); that will remove the new line or remove .split(',') .这将删除新行或删除.split(',')

 let transform = function (arr) { for (let i = 0; i < arr.length; i++) { if (arr[i] === '--discard-next') { let result = arr.slice(0, i) + "," + arr.slice(i + 2); let newArray = result; return newArray; } else if (arr[i] === '--discard-prev') { let result = arr.slice(0, i - 1) + "," + arr.slice(i + 1); let newArray = result; return newArray; } else if (arr[i] === '--double-next') { let result = arr.slice(0, i) + "," + arr[i + 1] + "," + arr.slice(i + 1); let newArray = result; return newArray; } else if (arr[i] === '--double-prev') { let result = arr.slice(0, i) + "," + arr[i - 1] + "," + arr.slice(i + 1); let newArray = result; return newArray; } } }; console.log('[',transform([1, 2, 3, '--double-next', 4, 5]),']'); // => [1, 2, 3, 4, 4, 5] console.log('[',transform([1, 2, 3, '--discard-prev', 4, 5]),']'); // => [1, 2, 4, 5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM