user4602620
user4602620

Reputation:

Filter objects array

I got an array like this:

const xx = [
  {
    name: "Alex",
    income: 324300,
    total: 3030000
  },
  {
    name: "Snake",
    income: 3433000,
    total: 34323000
  }
];

I want to check if the income is larger than 300000 and if it is then their name would be stored or logged.

This is what i have tried, i dont fully understand how it works so..

var f = {};
for (var i = 0, len = xx.length; i < len; i++) {
    f[xx[i].id] = xx[i];
    if(f[income]>=500000){
        console.log(xx[i]);
    }
}

Upvotes: 0

Views: 53

Answers (3)

kevin ternet
kevin ternet

Reputation: 4612

in ES5 you can use filter to to obtain an array populate with elements having total > 300000

var data = [
  {
    name: "Alex",
    income: 324300,
    total: 30000
  },
  {
    name: "Snake",
    income: 3433000,
    total: 34323000
  }
];
var result = data.filter(x => x.total > 300000);
console.log(result);

Upvotes: 1

naortor
naortor

Reputation: 2089

You can use Array.filter for filtering the array and Array.forEach for iterating over it to console.log or w/e you want to do with it.

const xx = [
  {
    name: "Alex",
    income: 324300,
    total: 3030000
  },
  {
    name: "Snake",
    income: 3433000,
    total: 34323000
  },
  {
    name: "Wake",
    income: 4,
    total: 3
  }
];

xx.filter(function(x) {
  return x.income > 30000;
}).forEach(function(x){
  console.log(x)
});

Upvotes: 2

Geeky
Geeky

Reputation: 7498

Use filter for this Check this snippet

const xx = [
  {
    name: "Alex",
    income: 324300,
    total: 3030000
  },
  {
    name: "Snake",
    income: 3433000,
    total: 34323000
  }
];
xx.filter(function(item){
  if(item.income>324300)
    alert(item.name);
})

Hope this helps

Upvotes: -2

Related Questions