简体   繁体   English

使用转发器值Javascript最小化数组

[英]Minimize an array with repeaters values Javascript

I'm trying to do something interesting in JavaScript, but I can't. 我正在尝试用JavaScript做一些有趣的事情,但我做不到。 This is my input: 这是我的意见:

 var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];

So my output is that only get differents values and go in a new vector. 所以我的输出是只获得不同的值并进入一个新的向量。

 var Output = SomeFunction(Input);

This is what I want: 这就是我要的:

Output = ['a','b','c','a','c','d'];

Y tried with this, but don't work aswell: Y尝试了这个,但不工作,以及:

function SomeFunction(input){
var out= [];
 for (var i = 0; i < input.length - 1; i++) {
  if(input[i] == input[i+1]){
   out.push(input[i]);       
  }
 }
 return out;
}

You can use filter() 你可以使用filter()

 var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd']; input = input.filter(function(v, i, arr) { return arr[i - 1] !== v; //compare with the previous value }) document.write(JSON.stringify(input)); 

Try like this 试试这样吧

var out= [];
var i = 0;
 for (i = 0; i < input.length - 1; i++) {
  if(input[i] != input[i+1]){
   out.push(input[i]);       
  }
 }
if (out[out.length-1] !== input[i]) 
   out.push(input[i]);     

You can use backreferencing regex 您可以使用反向引用正则表达式

  1. Convert the array to string by using join , so that regex can be used on it 使用join将数组转换为字符串,以便可以在其上使用正则表达式
  2. Use backreferencing regex to remove the consecutive elements with replace() 使用反向引用正则表达式删除带有replace()的连续元素
  3. Convert back the string to array using split 使用split将字符串转换回数组

(\\w)\\1* Explanation (\\w)\\1*解释

 var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd']; var str = input.join(''); input = str.replace(/(\\w)\\1*/g, '$1').split(''); console.log(input); document.write('<pre>' + JSON.stringify(input, 0, 2) + '</pre>'); 

You can do a filter like 你可以像这样做一个过滤器

 var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd', 'e']; var Output = SomeFunction(Input); function SomeFunction(input) { var out = input.filter(function(value, i) { return value !== input[i + 1] }); return out; } output.innerHTML = JSON.stringify(Output) 
 <pre id="output"><pre> 

 var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d']; var Output = SomeFunction(Input); function SomeFunction(input){ var out= []; for (var i = 1; i < input.length; i++) { if(input[i] != input[i-1]){ out.push(input[i-1]); } } out.push(input[input.length - 1]); return out; } alert(Output); 

How about this: 这个怎么样:

 var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd']; function SomeFunction(input) { var out = []; var initStr = input[0]; console.log(initStr) for (var i = 1; i < input.length; i++) { if (input[i] === initStr) { } else { out.push(input[i - 1]); initStr = input[i]; } } out.push(input[i - 1]); console.log(out); } SomeFunction(Input) 

Example using array filters. 示例使用数组过滤器。

Pretty simple as long as the array is not too large and could rather easily be extended to compare some property in an array of objects. 非常简单,只要数组不是太大,并且可以更容易地扩展以比较对象数组中的某些属性。 With a larger array it might be faster though to do a for loop instead as in some of the other answers. 对于一个更大的数组,它可能会更快,但在一些其他答案中做一个for循环。

 var Input = ['a', 'b', 'c','a', 'b', 'a', 'b', 'c','c'] var Output = Input.filter(function(value,index) { return Input[index - 1] != value; }); 

function SomeFunction(input) {
var out= [];
   out.push(input[i]);         
   for (var i = 0; i < input.length - 1; i++) {
      if(input[i] !== out[out.length-1]){
         out.push(input[i]);         
      }
   }
   return out;
}

Worked for me: 为我工作:

function SomeFunction(input){
var out= [];
 for (var i = 0; i < input.length; i++) {
  if(input[i] !== input[i+1]){
   out.push(input[i]);       
  }
 }
 return out;
}

Be careful with the name of the varible "input". 小心变量“输入”的名称。 It's not "Input", it's "input". 它不是“输入”,而是“输入”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM