Is there a better way to do this code? Perhaps using filter or reduce?

Viewed 125

The code I have is correct, I'm just curious if there is a shorter way to do this?

I think a filter would be the best method but I'm not so sure how to use it.

function tallEnoughToRide(group) {
  var result = [];
  for (var i = 0; i < group.length; i++) {
    if (group[i].heightInInches >= 48) {
      result.push(group[i].name);
    }
  }
  return result; //should return a list of people taller than 48 inches
}

var groupA = [{
    name: "Mia",
    age: 10,
    heightInInches: 52
  },
  {
    name: "Jaya",
    age: 9,
    heightInInches: 45
  },
  {
    name: "Kiana",
    age: 10,
    heightInInches: 55
  },
  {
    name: "Alex",
    age: 11,
    heightInInches: 48
  }
];
console.log(tallEnoughToRide(groupA));

3 Answers

You need two things

  1. .filter()

var groupA = [{
    name: "Mia",
    age: 10,
    heightInInches: 52
  },
  {
    name: "Jaya",
    age: 9,
    heightInInches: 45
  },
  {
    name: "Kiana",
    age: 10,
    heightInInches: 55
  },
  {
    name: "Alex",
    age: 11,
    heightInInches: 48
  }
];

console.log(groupA.filter(ele => ele.heightInInches >= 48))

You can see in the above snippet, the filter method is filtering out objects based on the condition.

  1. .map()

var groupA = [{
    name: "Mia",
    age: 10,
    heightInInches: 52
  },
  {
    name: "Jaya",
    age: 9,
    heightInInches: 45
  },
  {
    name: "Kiana",
    age: 10,
    heightInInches: 55
  },
  {
    name: "Alex",
    age: 11,
    heightInInches: 48
  }
];
console.log(groupA.map(obj => obj.name));

You can see in the above snippet, the map method is mapping out objects based on the key provided.

Now you can combine both .filter() and .map() together.

function tallEnoughToRide(group) {

  return group.filter(ele => ele.heightInInches >= 48).map(obj => obj.name)
}

var groupA = [{
    name: "Mia",
    age: 10,
    heightInInches: 52
  },
  {
    name: "Jaya",
    age: 9,
    heightInInches: 45
  },
  {
    name: "Kiana",
    age: 10,
    heightInInches: 55
  },
  {
    name: "Alex",
    age: 11,
    heightInInches: 48
  }
];
console.log(tallEnoughToRide(groupA));

You can use the combination of filter() and map():

console.log(groupA.filter(obj => obj.heightInInches >= 48).map(obj => obj.name));

The short answer, yes there actually is. Compute wise they are very similar. But the readability is much better. In one line it's very clear what it should do.

let tallEnoughToRide = groupA.filter(e => e.heightInInches >= 48)

Extracting the names only looks like:

let tallEnoughToRide = groupA.reduce((acc, curr) => {
    if(curr.heightInInches >= 48)
        acc.push(curr.name)
    return acc;
}, []);

The readability of the reduce function isn't actually very good. Again, performance is very similar because it's still O(n)

It's up to you what to use. What you prefer. Long and explaining code or shorthanded code.

I saw this line or similar, like the following in this thread very often:

let result = groupA.filter(p => p.heightInInches >= 48).map(p => p.name)

Don't actually use this if you care about performance, which you should if you didn't already. Because you are giving up on compute performance to O(2n). You are also using more memory since you create one extra array.

Related