Dee J. Dee J. - 4 months ago 8
AngularJS Question

How to remove an array element using a specific value

Suppose I have an array structured like this:

{ q1: true, q2: false, q3: false, q4: true, q5:true }


Is there a way to remove all the elements that has "false" for a value in AngularJS?

I tried using splice() by getting the index number, with no luck. Now I'm looking for a way where I don't have to use a specific index number.

Any help would be appreciated. Thank you.

Answer

Simple JavaScript approach

var r = {
  q1: true,
  q2: false,
  q3: false,
  q4: true,
  q5: true
};

for (a in r) {
  if (!r[a]) {
    delete r[a]
  }
}

console.log(r);

Comments