1

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: [] }
Zr Classic
  • 303
  • 4
  • 14

3 Answers3

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)
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • so, the map is not to use on this situation ?? how about with forEach?? or filter?? – Zr Classic Sep 24 '18 at 16:27
  • @ZrClassic https://stackoverflow.com/q/34887973/1048572, https://stackoverflow.com/q/11731072/1048572 – Bergi Sep 24 '18 at 16:30
  • **map** takes an array and turns it into something new without removing or adding items. **filter** just removes items that don't match the filter, and **forEach** loops over the items like a `while` or `for` loop, so that is why I feel reduce is better for this situation. – Get Off My Lawn Sep 24 '18 at 16:30
  • @GetOffMyLawn Given that you are using `push`, a `for` loop would have been appropriate as well :-) – Bergi Sep 24 '18 at 16:31
  • **reduce** on the other hand will take an array and reduce it to a single item, in this case that would be an object. – Get Off My Lawn Sep 24 '18 at 16:31
  • @GetOffMyLawn I don't see what's cool about `reduce` if you don't use it for functional programming (or there is a good reason to have a single expression). But if you want to be hip, use a `for (const item of items)` loop :-) – Bergi Sep 24 '18 at 16:34
  • @ZrClassic I have updated with more ways that you asked about. – Get Off My Lawn Sep 24 '18 at 16:51
2

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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      
}}
adam.k
  • 622
  • 3
  • 15