codecat
codecat

Reputation: 145

How to get the array of objects based on condition

How to get the array of objects based on condition in javascript.

I have array object obj in which each objects w1,w2...wn should have count greater than 2.

How to filter the array object based on object key in javascript.

function getObject (obj1){
  var result = obj1.filter(e=> e.w1.count > 2 && e.w2.count > 2);
  return result;
}
var output = this.getObject(obj1);

var obj1=[
 {
"memberid": "s1",
"w1":{"count": 1, "qty": 1},
"w2":{"count": 0, "qty": 0},
 ... wn
"totalcount": 1
 },
{
"memberid": "s2",
"w1":{"count": 2, "qty": 2, "amount": 400.0},
"w2":{"count": 1, "qty": 2, "amount": 503.0},
 ... wn
"totalcount": 5
},
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
 ... wn
"totalcount": 6
}
]

Expected Output:

[
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
 ... wn
"totalcount": 6
}
]

Upvotes: 0

Views: 71

Answers (3)

Lord Midi
Lord Midi

Reputation: 804

function getObject (obj1) {
  var result = obj1.filter((e) => {
    var isValid = false;
    var i = 1;
    while (e['w' + i]) {
      if (e['w' + i].count > 2) {
        isValid = true;
      } else {
        isValid = false;
        break;
      }
      i++;
    }
    return isValid;
  });
  return result;
}

Upvotes: 0

Nick
Nick

Reputation: 147166

You can filter your array based on every value in each object either not being an object, or if it is an object, having a count greater than 2:

const obj1 = [{
    "memberid": "s1",
    "w1": {
      "count": 1,
      "qty": 1
    },
    "w2": {
      "count": 0,
      "qty": 0
    },
    "totalcount": 1
  },
  {
    "memberid": "s2",
    "w1": {
      "count": 2,
      "qty": 2,
      "amount": 400.0
    },
    "w2": {
      "count": 1,
      "qty": 2,
      "amount": 503.0
    },
    "totalcount": 5
  },
  {
    "memberid": "s3",
    "w1": {
      "count": 3,
      "qty": 2,
      "amount": 0.0
    },
    "w2": {
      "count": 3,
      "qty": 4,
      "amount": 503.0
    },
    "totalcount": 6
  }
];

const out = obj1.filter(o => Object.values(o).every(v => typeof v != 'object' || v.count > 2));

console.log(out);

Upvotes: 1

jonatjano
jonatjano

Reputation: 3738

you need to iterate over the object keys, filtering out the invalid ones

function getObject(obj1) {
  // filter
  return obj1.filter(e =>
    // based on the entries [key, value]
    Object.entries(e)
    // filter out entries where key is not a w followed by a number  
    .filter(val => val[0].match(/w\d+/))
    // if every selected entry as a count > 2
    .every(val => val[1].count > 2)
  );
}

const obj1=[{memberid:"s1",w1:{count:1,qty:1},w2:{count:0,qty:0},totalcount:1},{memberid:"s2",w1:{count:2,qty:2,amount:400},w2:{count:1,qty:2,amount:503},totalcount:5},{memberid:"s3",w1:{count:3,qty:2,amount:0},w2:{count:3,qty:4,amount:503},totalcount:6}];

const output = this.getObject(obj1);
console.log(output)

docs of usefull functions : Object.entries, Array.filter, Array.every

Upvotes: 0

Related Questions