我有三个包含CSS类名字符串的数组。
我需要从每个数组中取一个字符串,并将其与其他两个数组中的值组合,以形成所有可能的唯一字符串组合。
以下是我需要转换的数组:
var array_one = [
".cat-616",
".cat-1146",
".cat-1144"
];
var array_two = [
".hair-light-brown",
".hair-red"
];
var array_three = [
".eyes-hazel",
".eyes-dark-brown",
".eyes-blue-green"
];
我需要形成的字符串是:
'.cat-616.hair-light-brown.eyes-hazel',
'.cat-616.hair-light-brown.eyes-dark-brown',
'.cat-616.hair-light-brown.eyes-blue-green',
'.cat-616.hair-light-red.eyes-hazel',
'.cat-616.hair-light-brown.eyes-hazel',
'.cat-616.hair-light-brown.eyes-dark-brown',
'.cat-616.hair-light-brown.eyes-blue-green',
'.cat-1146.hair-light-brown.eyes-hazel',
'.cat-1146.hair-light-brown.eyes-dark-brown',
'.cat-1146.hair-light-brown.eyes-blue-green',
'.cat-1146.hair-red.eyes-hazel'
…etc.
如何迭代这些数组以形成字符串组合?我的想法是Lodash可以用来转换这些数组吗?
答案 0 :(得分:1)
有更多'sciency'方法可以做到这一点,但为什么这样的东西不会起作用呢?
for (var i = 0; i < array_one.length; i += 1) {
for (var j = 0; j < array_two.length; j += 1) {
for(var k = 0; k < array_three.length; k += 1) {
console.log(array_one[i] + array_two[j] + array_three[k]);
}
}
}