使用rest和array旋转字符串。 javascript

时间:2018-06-27 15:10:38

标签: javascript arrays string rest rotation

我试图了解rest数组,箭头函数的捷径,我想我将能够保留它。要么我错过了reduce函数的迭代方式,要么错过了精确的机制,将... word与acc参数叠加,剩下的... acc],

const getWordRotations = word =>
  [...word].reduce(
    acc => [acc[0].substring(1) + acc[0].substring(0, 1), ...acc],
    [word]
  ); 

我试图反转箭头功能

const getWordRotations = function(word) {
  return [...word].reduce(function(acc) {
    return [acc[0].substring(1) + acc[0].substring(0, 1), ...acc], [word];
  });
};
const groupCitiesByRotatedNames = cities =>
  cities.reduce((acc, city) => {
    const cityGroup = acc.find(item =>
      getWordRotations(city.toLowerCase()).includes(item[0].toLowerCase())
    );
    console.log(cityGroup);
    cityGroup
      ? acc.splice(acc.indexOf(cityGroup), 1, [...cityGroup, city])
      : acc.push([city]);
    return acc;
  }, []);

const test = groupCitiesByRotatedNames([
  "Tokyo",
  "London",
  "Rome",
  "Donlon",
  "Kyoto",
  "Paris"
]);

console.log("testy", test);

0 个答案:

没有答案