最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ifelse statement in map function? - Stack Overflow

programmeradmin5浏览0评论

I am here want to use map function in javascript to loop a type data array,but i get error for these syntax below :

function porti(scores) {
const test = scores.map(pass, fail) => {
    if (scores < 75){
      test.fail
    } else {
      test.pass
    }
    return {pass, fail}
  }

}

output must be, if scores < 75 : fail, else : pass

console.log(porti([80, 45, 90, 65, 74, 100, 85, 30]));
// { pass: [ 80, 90, 100, 85 ], fail: [ 45, 65, 74, 30 ] }

console.log(porti([]));
// { pass: [], fail: [] }

I am here want to use map function in javascript to loop a type data array,but i get error for these syntax below :

function porti(scores) {
const test = scores.map(pass, fail) => {
    if (scores < 75){
      test.fail
    } else {
      test.pass
    }
    return {pass, fail}
  }

}

output must be, if scores < 75 : fail, else : pass

console.log(porti([80, 45, 90, 65, 74, 100, 85, 30]));
// { pass: [ 80, 90, 100, 85 ], fail: [ 45, 65, 74, 30 ] }

console.log(porti([]));
// { pass: [], fail: [] }
Share Improve this question asked Sep 24, 2018 at 16:21 Zr ClassicZr Classic 3135 silver badges14 bronze badges 4
  • Which error do you got? – Calvin Nunes Commented Sep 24, 2018 at 16:25
  • 1 test.fail is supposed to do what? map is the wrong thing here.... – epascarello Commented Sep 24, 2018 at 16:25
  • yes maybe, what else i should use then ?? :) – Zr Classic Commented Sep 24, 2018 at 16:28
  • you are missing a bracket at: .map(, must be .map(( – Jonas Wilms Commented Sep 24, 2018 at 17:00
Add a ment  | 

3 Answers 3

Reset to default 4

I think reduce would be better for this situation. This will allow us to reduce the array to an object of two item arrays.

let items = [80, 45, 90, 65, 74, 100, 85, 30]

let result = items.reduce((obj, item) => {
  item < 75 ? obj.fail.push(item) : obj.pass.push(item)
  return obj
}, {pass:[], fail:[]})

console.log(result)

If you wanted to use filter you could...

let items = [80, 45, 90, 65, 74, 100, 85, 30]

let result = {
  pass: items.filter(i => i >= 75),
  fail: items.filter(i => i < 75)
}

console.log(result)

And here is how we can do it with forEach...

let items = [80, 45, 90, 65, 74, 100, 85, 30]

let result = {pass:[], fail:[]}
items.forEach(itm => itm < 75 ? result.fail.push(itm) : result.pass.push(itm))

console.log(result)

You could integrate the check as ternary for getting the key for pushing.

function porti(scores) {
    var result = { pass: [], fail: [] },
        score;

    for (score of scores) {
        result[score < 75 ? 'fail': 'pass'].push(score);
    }
    return result
}

console.log(porti([80, 45, 90, 65, 74, 100, 85, 30]));
console.log(porti([]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

As mentioned above .map() should best be saved for when you are looking to return an array by manipulating a previous array. If you don't wish to use a vanilla for loop. You could try this

const testScores = [...someArray of numbers]
function porti(tesScores) {    
const result = {
   pass: [],
   fail: []
}
    for (let score of testScores) {
        if (score < 75) {
           result.fail.push(score)
         } else {
           result.pass.push(score)
         }
  return result      
}}
发布评论

评论列表(0)

  1. 暂无评论