Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

Javascript - How i do compare two objects of an array?

I have some objects into an array. I want to compare object[0].position with object[1].position and If These two values were equal, object[0].position = 0. how can I do this?

`

function carInfo(name) {
  this.name = name;
  this.position = 0;
}

let numOfCars = +prompt("Enter Number of Cars: ");
for (let i = 0; i < numOfCars; i++) {
  carNames.push(new carInfo(prompt(`Enter Name of Car No.${i + 1} : `)));
}

let str = "*";
str += str.repeat(300);
console.log(str);

function shuffleArray(array) {
  for (let i = 0; i < array.length; i++) {
    let temp = Math.floor(Math.random() * 10 + 1);
    array[i].position += temp;
    if (array[i].position >= 300) {
      alert("game end! " + '"' + array[i].name + '"' + " win");
      break;
    }
    
  }
  return array;
}

`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

by position of them. I want say:

If(object[i].position === object[i+1].position){
 object[i].position = 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...