var cubes = [
['a','b','c','d','e'],
['f','g','h','i','j'],
['k','l','m','n','o']
]
var tmp = []
for(var i = 0; i < cubes.length; i++)
{
var cube = cubes[i];
var step = 0
for(var j = 0; j < cube.length; j++)
{
tmp.push((cube[j])) // DON'T KNOW WHAT TO DO HERE
}
}
我想要每个数组的第一个字母加入,然后取每个数组的第二个字母,然后是第三个字母,将它们组合起来制作一个加密的单词。 每个加密的word.length应该是最多5个字符。如果从阵列中读取,它将创建加密。
上述示例输出:afkbg lchmd inejo
答案 0 :(得分:0)
/**
* cubesToWord
*
* @param {string[][]} cubes
* @param {number} [maxLength=5]
* @returns {string[]}
*/
function cubesToWord(cubes, maxLength) {
if (maxLength === void 0) {
maxLength = 5;
}
var words = [''];
var maxX = cubes.map(function(value) {
return value.length;
}).reduce(function(prev, curr) {
return (curr > prev ? curr : prev);
}, 0);
for (var x = 0; x < maxX; x++) {
for (var y = 0; y < cubes.length; y++) {
if (cubes[y] != void 0 ? cubes[y][x] == void 0 : true) {
continue;
}
words[words.length - 1] += cubes[y][x];
if (words[words.length - 1].length >= maxLength) {
words.push("");
}
}
}
return words.filter(function(val) {
return val.length > 0;
});
}
//TEST
var cubes = [
['a', 'b', 'c', 'd', 'e'],
['f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm', 'n', 'o']
];
console.log(cubesToWord(cubes, 3));
&#13;
答案 1 :(得分:0)
reduce
,map
和{{1> 二维数组 逐列 >> }}
join
现在使用var flattenStr = cubes[0].reduce( (a, c, i1) => a.concat( cubes.map( ( s, i2 ) => s[i1] ) ) , [] ).join("");
match
<强>演示强>
var output = flattenStr.match(/.{1,5}/g)
&#13;
答案 2 :(得分:0)
var rectangle = cubes [0] .reduce((a,c,i1)=&gt; a.concat(cubes.map((s,i2)=&gt; s [i1])),[])。加入(&#34;&#34);
var encryption = rectangle.match(/.{1,5}/g);
console.log("Your encryption is as follows: \n" + encryption.join(' '));
答案 3 :(得分:0)
您可以生成一个包含连接列的数组,并生成一个包含所需长度的字符串的数组。
var cubes = [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'n', 'o']],
size = 5,
result = cubes
.reduce((r, a) => a.map((v, i) => (r[i] || '') + v), [])
.join('')
.match(new RegExp('.{1,' + size + '}', 'g'));
console.log(result);
&#13;
答案 4 :(得分:0)
我将为学生采取最简单的方法。
<div id="wrapper">
<div id="content">
<div id="closeButton" onClick="parent.$.fancybox.close();">
×
</div>
</div>
</div>
这就像人类一样,从上到下读取数组。
答案 5 :(得分:0)
const cubes = [
['a','b','c','d','e'],
['f','g','h','i','j'],
['k','l','m','n','o']
]
let tmp = [];
let word = [];
for(let j=0;j<cubes[0].length;j++) {
for(let i=0;i<cubes.length;i++) {
word.push(cubes[i][j])
if(word.length == 5) {
tmp.push(word.join(''));
word = []
}
}
}
console.log(tmp.join(' '))