使用JavaScript在空间后反转数组

时间:2019-04-03 08:04:21

标签: javascript arrays reverse

我有一个想要反转的数组

  

['I',',','l','o','v','e','','E','a','r','t','h'] ;

我想这样扭转它

  

[“ I”,“”,“ e”,“ v”,“ o”,“ l”,“”,“ h”,“ t”,“ r”,“ a”,“ E”]

问题是我可以使用private static ObjectMapper configureMapper(ObjectMapper mapper) { mapper.setDefaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPY, JsonInclude.Include.NON_NULL)); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return mapper; } 反转它,但是它反转霍尔阵列,我想在哪里有空间之后反转。 谢谢

4 个答案:

答案 0 :(得分:9)

好吧,有些肮脏的解决方案可能与此类似:

var sentence = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];
    
var reversed = sentence.join('').split(' ').map(word => word.split('').reverse().join('')).join(' ').split(''); 
    
console.log(reversed);

答案 1 :(得分:1)

您可以将其设置为字符串,然后将其拆分并设置为flatMap

const arr = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];

const res = arr
              .join('')
              .split(' ')
              .flatMap(e => (e + ' ').split('').reverse())
              .slice(1);

console.log(res);

答案 2 :(得分:1)

您可以采用递归方法并收集子数组并反转此值。

function swap(array, i = 0, temp = []) {
    if (i >= array.length || array[i] === ' ') {
       temp.forEach((v, j) => array[i - j - 1] = v);
       temp = [];
    } else {
        temp.push(array[i]);
    }
    if (i >= array.length) return array;
    return swap(array, i + 1, temp);
}

var array = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];

console.log(swap(array));
.as-console-wrapper { max-height: 100% !important; top: 0; }

另一种方法是更改​​返回值。

function swap(array, i = 0, temp = []) {
    if (i >= array.length) return array;        
    swap(array, i + 1, array[i] === ' ' ? temp = [] : (temp.push(array[i]), temp));
    if (temp.length) array[i] = temp.shift();
    return array;
}

var array = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];

console.log(swap(array));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:1)

您可以使用Array.reduceArray.splice逐个字母地遍历数组(避免创建多个临时数组):

var sentence = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];

var reversed = sentence.reduce((acc, letter) => {
    if (letter === ' ') {
        acc.result.push(letter);
        acc.insertIndex = acc.result.length;
    } else {
        acc.result.splice(acc.insertIndex, 0, letter);
    }
    return acc;
}, {result: [], insertIndex: 0});

console.log('reversed:', reversed.result);

或结合使用Array.joinString.splitArray.reverse(创建多个临时数组):

var arr = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];
var sentence = arr.join(''); // "I love Earth"
var words = sentence.split(' '); // [ "I", "love", "Earth" ]
var reverseParts = words.map(w => w.split('').reverse().join('')); // [ "I", "evol", "htraE" ]
var reverseSentence = reverseParts.join(' '); // "I evol htraE"
var reversed = reverseSentence.split(''); // [ "I", " ", "e", "v", "o", "l", " ", "h", "t", "r", "a", "E" ]
console.log(reversed);

可以合并某些行,以使其更短且更清晰:

var arr = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'E', 'a', 'r', 't', 'h'];
var words = arr.join('').split(' '); // [ "I", "love", "Earth" ]
var reverseParts = words.map(w => w.split('').reverse().join('')); // [ "I", "evol", "htraE" ]
var reversed = reverseParts.join(' ').split(''); // [ "I", " ", "e", "v", "o", "l", " ", "h", "t", "r", "a", "E" ]
console.log(reversed);