在数组JS中找到三重复制品

时间:2016-05-13 10:09:33

标签: javascript arrays

目前的代码不起作用。我想提醒所有3个相同数字的序列。

function myFunction() {
    var fruits = [3, 3, 3, 5, 4, 6, 8, 9];
    var a = fruits.sort();
    document.getElementById("demo").innerHTML = a;
    for(var i = 0, j = 1, k = 2; i < fruits.length, j < fruits.length, k < fruits.length; i++, j++, k++){
        if(fruits[i].valueOf() == fruits[j].valueOf() == fruits[k].valueOf()){
            alert(fruits[i].valueOf()+" "+fruits[j].valueOf() +" "+fruits[k].valueOf());
        }
    }
}

6 个答案:

答案 0 :(得分:1)

您可以尝试以下选项:

for(var i = 0; i < fruits.length; i++){
    if(fruits[i] === fruits[i+1] && fruits[i] === fruits[i+2]){
       //msg
       alert('found!');
    }
}

答案 1 :(得分:1)

嗯,你可以这样做,但从v0.0.1开始只适用于个位数。

var fruits = [3, 3, 3, 5, 4, 6, 8, 9, 6, 6, 6],
    triple = fruits.join("").match(/(\d)\1\1/g).map(e=>e.split("").map(e=>e*1));

document.write(JSON.stringify(triple));

答案 2 :(得分:0)

试试这个:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<div id="services">
		<div class="col-sm-8 col-sm-offset-2 text-center">
			<div class="row">
				<h1>Our Team</h1>
				<div class="separator2"></div>
				<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
				<div class="col-md-4 service">
					<div class="img-responsive">
						<img src="photos/service.jpeg" alt="Service">
					</div>
					<div class="service_description">
						<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
					</div>
				</div>
				<div class="col-md-4 service">
					<div class="img-responsive">
						<img src="photos/service.jpeg" alt="Service">
					</div>
					<div class="service_description">
						<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
					</div>
				</div>
				<div class="col-md-4 service">
					<div class="img-responsive">
						<img src="photos/service.jpeg" alt="Service">
					</div>
					<div class="service_description">
						<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
					</div>
				</div>
			</row>
		</div>
	</div>

它的输出将为3(因为元素连续出现3次)

答案 3 :(得分:0)

尝试这个

for(var i = 0; i < fruits.length; i++) {
if(( fruits[i+1] !== null ) && (fruits[i] === fruits[i+1])) {
    if(( fruits[i+2] !== null ) && (fruits[i] === fruits[i+2])) {
          // your message 
          alert('');
      }
   }
}

答案 4 :(得分:0)

使用无序扩展示例数组Array.sliceArray.indexOfArray.some函数的解决方案:

function myFunction() {
    var fruits = [3, 3, 5, 3, 4, 6, 8, 9, 9, 7, 9],
        len = fruits.length, k = 0, notEqual = false, sequence;

    fruits.sort();
    while (k <= len) {
        sequence = fruits.slice(k, k + 3);
        if (sequence.length < 3) break;
        notEqual = sequence.some((v, k, arr) => arr.indexOf(v) !== 0);
        if (!notEqual) {   // check if all numbers in the sequence are equal
            console.log(sequence);
            k += 2;
        } else {
            k += 1;
        }
    }
}

myFunction();

输出:

[3, 3, 3]
[9, 9, 9]

答案 5 :(得分:0)

我可以在代码中看到两个主要问题

1)如果循环中的条件:

if(fruits[i].valueOf() == fruits[j].valueOf() == fruits[k].valueOf())

这个条件总是假的,因为当它与两个值比较时,结果将为false或true然后再次将false和true与每次都为false的值进行比较,即(true === 3)result:false

2)我可以看到你正在使用警报valueOf方法你不需要你可以直接使用fruits [i]来获得价值。

function myFunction() {
var fruits = [3, 3, 3, 5, 4, 6, 8, 9];
var a = fruits.sort();
document.getElementById("demo").innerHTML = a;
for(var i = 0, i < fruits.length-3;i++){

        if((fruits[i] == fruits[i+1]) &&(fruits[i]== fruits[i+2])){
        alert(fruits[i]+" "+fruits[i+1] +" "+fruits[i+2]);
    }
}}