Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
how to find an element with the parameter as selected and output it in javascript?

I am newbie with javascript and meet a problem like this.

For example . there is a variable with a list like this

{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
}

Find an variable with an attack = 150 and return it in the function "findAMonsterByAttack" If there is no matching variable in the list of variable, the "findAMonsterByAttack" function returns null.

Here is my code :

function findAMonsterByAttack(monsters) {
var monsters = [
{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
},
// ...
];
var isfree = monsters.every(function(course, index) {
return course.attack ===150;
});

My idea is using the code every to check course and index, then return if the attach = 150. But I failed. Could you please help me with this case ? Thank you very much for your time.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If monsters is an array, you can use array.filter to return another array with of only the monsters with a matching criteria.

eg attack === 150.

Note that there could be multiple matches, so this would return an array with all of the monsters that match, (or an empty array if no monsters match).

const monsters = [{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
  },
  {
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
  },
  {
    name: 'z',
    attack: 150,
    speed: 90,
    hitpoint: 150,
  }
]

const a150 = monsters.filter(m => m.attack === 150)

console.log(a150)

If you only care about one monster, you could also use array.find. But this would return the first monster found and undefined if no matches are found.

const monsters = [{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
  },
  {
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
  },
  {
    name: 'z',
    attack: 150,
    speed: 90,
    hitpoint: 150,
  }
]

const a150 = monsters.find(m => m.attack === 150)

console.log(a150)

Note: array.every tests whether all monsters meet the criteria and returns a Boolean value. In your example this will return false because all monsters do not have an attack value of 150.

about 4 years ago · Juan Pablo Isaza Report

0

Is this what you want?

function findAMonsterByAttack() {
var monsters = [
{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
}
// ...
];

for ( m of monsters ) {
  if ( m.attack == 150 )
    return m
}
return "Monster with attack 150 not found"
}

console.log(findAMonsterByAttack())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!