-1

How can I use filtering an array of objects

1
  • [2,6,7,34,78,31,89,64].filter(x => x % 2 !== 0) Commented Mar 29, 2021 at 12:01

2 Answers 2

2

Use array.filter()

The below should work no problem

const oddNums = (array) => {
 const results = array.filter(num => num % 2 !== 0)
 return results 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice!, Can further simplify to array.filter(num => num % 2)
0

You can try using for loop,

 let a = [2,6,7,34,78,31,89,64];
 let b = [];

for(var i=0;i<=a.length-1;i++){
     if(a[i] % 2!==0){ 
         b.push(a[i]); 
    }
 }
 console.log(b);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.