0

I have to creater function ( lets call it filterItems), that will filter 2 arrays ( take all the numbers above 0) and the result should return new array with all the number above 0 from 2 arrays.

f.e input

[1, -2, 3] [1, 0]

output

[1, 3, 1]

I tried by using map, but it failed.

function filterItems(arr,array) {
return arr.map(number => arr > 0 , array > 0)
}

Thank eveyrone for their time and i will be grateful for any tips.

5
  • 2
    You can simply merge the two arrays and then filter all items > 0: const result = [...array1, ...array2].filter(item => item > 0)? Commented Jan 26, 2021 at 14:25
  • for exercise i have to create function called "filterItems". so i dont know if it will count ( in totally new, i know css and html, but im learning jss to uderstand what exacly going with apps). Commented Jan 26, 2021 at 14:28
  • 1
    It does not change anything: function filterItems(arr, array) { return [...arr, ...array].filter(item => item > 0) }. It is exactly the same concept, but you use the resulting array as a the returned value for a function instead of assigning it to a variable. Commented Jan 26, 2021 at 14:31
  • Tahnk you!!! I wish i be that smart. Commented Jan 26, 2021 at 14:33
  • 1
    It has nothing to do with being smart; it is a matter of experience and familiarity with the language... [mode:grumpy-grandpa] something that you can acquire quicker, if you try a little bit harder to solve the exercises by yourself rather than giving up too quickly and looking for someone else's support.[/mode:grumpy-grandpa] ;) Commented Jan 26, 2021 at 14:36

1 Answer 1

1

You can concatenate the arrays and then filter out the numbers <= 0.

This approach is a little more extended because receives the handler for the function Array.prototype.filter.

const filterItems = (arr, array, fn = (n) => n > 0) => arr.concat(array).filter(fn);

console.log(filterItems([1, -2, 3], [1, 0]));
console.log(filterItems([1, -2, 3], [1, 0], (n) => n <= 0));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.