所以我在这个问题上停留了几天了: 我的输入是这样:
"255,255,255,10,251,91,31,4,220,220,220,1"
它是一个具有3个不同RGB值的字符串,还带有一个数字,指示其数量从最高到最低。 您可以将字符串从上面翻译为:
"RED1, GREEN1, BLUE1, QUANTITY1, RED2, GREEN2 ... "
虽然1代表第一种颜色,而2代表第二种颜色,依此类推。
我需要返回的是第一个没有数量的颜色。 所以我的输出应如下所示:
"255,255,255,251,91,31,220,220,220"
我尝试了很多事情,其中之一就是该功能:
var firstC, secondC, thirdC;
var pointer = "firstC";
function getEachColorValue(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
endIndex = i;
if (pointer == "firstC") {
firstC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "secondC";
} else if (pointer == "secondC") {
secondC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "thirdC";
} else if (pointer == "thirdC") {
thirdC = color.substring(startIndex, endIndex);
startIndex = endIndex;
pointer = "firstC";
}
}
}
}
这会将RED1
中的firstC
,GREEN1
中的secondC
和BLUE1
中的thirdC
推入
我考虑过一次,使用函数将firstC,secondC,thirdC写入数组,然后将它们重置。然后将col3
切成没有第一个调色板的子字符串,然后重复。
// Global variables
var stringHolder;
var newString;
var counter = 0;
var colorSetHT = new Array;
// main
createSubstring(col3);
function createSubstring(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
counter++;
endIndex = i;
}
if (counter == 4) {
stringHolder = color.substring(startIndex, endIndex);
alert(stringHolder);
newString = color.substring(endIndex+1, color.length);
getEachColorValue(stringHolder);
colorSetHT.push(firstC, secondC, thirdC)
colorReset();
counter = 0;
stringHolder = "";
// createSubstring(newString); // ?
}
}
}
我已经尝试过了,但是到目前为止还没有运气。我什至试图递归地做到这一点。
我对Java语言有点陌生(实际上是为Extendscript做的),我认为有一种更简单的方法,可以与split/slice
一起使用,但是我还没有找到。我试图使它尽可能容易和快速地阅读,如果可以提供任何进一步的信息,请告诉我!
答案 0 :(得分:0)
这是使用split
的方法。
var input = "255,255,255,10,251,91,31,4,220,220,220,1";
var inputArray = input.split(",");
var outputArray = [];
for(let i = 0;i<inputArray.length;i++)
{
if(i%4 != 3)
{
outputArray.push(inputArray[i]);
}
}
var output = outputArray.join(",");
console.log(output);
答案 1 :(得分:0)
尝试
let output = input.split(',').filter((x,i)=> i%4-3).join();
let input="255,255,255,10,251,91,31,4,220,220,220,1"
let output = input.split(',').filter((x,i)=> i%4-3).join();
console.log(output);
答案 2 :(得分:0)
使用一个简单的for
循环,如下所示:
const str = "255,255,255,10,251,91,31,4,220,220,220,1";
var colors = str.split(",");
var x = Math.floor(colours.length / 4);
while (x--) {
colors.splice((x + 1) * 4 - 1, 1);
}
colors = colors.join(",");
console.log(colors);