0

Below is the array of objects. what we have to do is to

  1. create an array that contains all the non-empty elements of array arr with value not equal to null, NaN, 'undefined' and 0
  2. remaining in the second array
var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  {},
  { id: null },
  { id: NaN },
  { id: "undefined" }
];


what I have tried is


var obj1 = {};
 var prop1 = [];
 var prop2 = [];
  arr.forEach(el=>{
      if(el.id!==0 || el.id!==null || el.id!==undefined || el.id!==NaN){
          prop1.push(el)
      }
      else{
          prop2.push(el)
      }
  })
  console.log(prop1)
  
  console.log(prop2)

but it is not working

output I receive -

1] [{id: 15}, {id: -1}, {id: 0}, {id: 3}, {id: 12.2}, {}, {id: null}, {id: null}, {id: "undefined"}]

2] []

expected -

1] [{id: 0}, {id: null}, {id: "undefined"}]

2] [{id: 15}, {id: -1}, {id: 3}, {id: 12.2}]

6
  • Please add what you are getting as an output when you console.log(prop1) and prop2 Commented Apr 7, 2020 at 3:38
  • Instead of writing "it is not working", please describe in detail what the expected outcome vs. actual outcome is. If you get any errors list them explicitly. Commented Apr 7, 2020 at 3:40
  • Think about all the things that satisfy (x !== 0 || x != null) You are currently thinking about all things. ;) You likely intended && instead of ||. Commented Apr 7, 2020 at 3:42
  • 1
    Do you understand why el.id!==0 || el.id!==null || el.id!==undefined || el.id!==NaN will always be true? Consider: (x !== 1 || x !== 2) You meant &&. Commented Apr 7, 2020 at 3:47
  • Did you REALLY mean to include the string "undefined" or did you just mean undefined? That seems a little bit weird. Commented Apr 7, 2020 at 3:54

3 Answers 3

2

You can cast to boolean (!el.id) which will handle most cases and you have to deal with "undefined" separately:

var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  {},
  { id: null },
  { id: NaN },
  { id: "undefined" }
];

var obj1 = {};
 var prop1 = [];
 var prop2 = [];
  arr.forEach(el=>{
      if(!el.id || el.id === "undefined"){
          prop1.push(el)
      }
      else{
          prop2.push(el)
      }
  })

  console.log(prop1);
  console.log(prop2);
  console.log(prop1.length);
  console.log(prop2.length);

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

1 Comment

It's all fine. prop2 represents non-empty elements so the count that gets returned is 4, whereas prop1.length returns 5 (you can run my script to see that)
1

Try this:

var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  {},
  { id: null },
  { id: NaN },
  { id: "undefined" }
];
let filterArray = arr.filter((el) => {return !el.id || [0,null,"undefined",NaN].includes(el.id)});
console.log(filterArray);
let filterArray1 = arr.filter((el) => {return el.id && ![0,null,"undefined",NaN].includes(el.id)});
console.log(filterArray1);

2 Comments

Edited to use .includes as we can add what we want in it for ease.
Edited further to handle blank entry!
0

why does it not work? Is the ordering incorrect or is there a JavaScript error?

The code looks fine at the first glance, you may use if (!el.id), which checks if the id is "falsy" (0, null, undefined, NaN, false)

You may also have a look at the methods "map" and "filter", https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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.