1

I am having trouble filtering through this array that contains objects. An explanation would be greatly appreciated.

Instructions:

// Using filter(), return only your friends of the array of people below. //Assign it to a variable called 'trueFriends'.

My Solution:

var trueFriends = [];

peopleIknow.filter(function(){
  for(var i = 0; i < peopleIknow.length; i++){
    if (peopleIknow[i].friend == true){
      trueFriends.push(peopleIknow[i]);
    }
  }
});

var peopleIknow = [
  { name: "Steve", friend: true },
  { name: "Dan", friend: false },
  { name: "Bart", friend: true },
  { name: "Sarah", friend: false },
  { name: "Michelle", friend: false },
  { name: "Holly", friend: true }
];
1

5 Answers 5

2

You should not use for loop in filter. Filter iterates through the array and return all the elements based on condition

var peopleIknow = [
  { name: "Steve", friend: true },
  { name: "Dan", friend: false },
  { name: "Bart", friend: true },
  { name: "Sarah", friend: false },
  { name: "Michelle", friend: false },
  { name: "Holly", friend: true }
];

var o = peopleIknow.filter(e=> e.friend );

console.log(o)

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

1 Comment

e.friend === true is not nesessary as e.friend itself is boolean.
1

Using a for loop inside a filter function defeats the purpose - try just using the filter function to iterate. You also shouldn't try to filter over an array before the array has been declared.

The first argument to the filter function is the item being iterated over, and you're only interested in the friend property, so you can extract only the friend property. If friend is true, then you want to return true to the filter function, so that it's included in your final array - else, return false. So, just return the friend property as is.

const peopleIknow = [
  { name: "Steve", friend: true },
  { name: "Dan", friend: false },
  { name: "Bart", friend: true },
  { name: "Sarah", friend: false },
  { name: "Michelle", friend: false },
  { name: "Holly", friend: true }
];

const trueFriends = peopleIknow.filter(({ friend }) => friend);
console.log(trueFriends);

Comments

0

Don't run a for loop in your filter function. Filter is so easy to use. If the result of the function passed to filter is true it adds that element to the return value. This is what you are looking for!

var peopleIknow = [
  { name: "Steve", friend: true },
  { name: "Dan", friend: false },
  { name: "Bart", friend: true },
  { name: "Sarah", friend: false },
  { name: "Michelle", friend: false },
  { name: "Holly", friend: true }
];

var trueFriends = [];

//                                         only the friends where person.friend is truthy 
trueFriends = peopleIknow.filter(person => person.friend);

console.log(trueFriends);

Comments

0

You may use filter() as well as reduce() to do the same.

Here is using reduce() method. Both reduce and filter runs for the function once for each element of the array.

var trueFriends = [];
var peopleIknow = [
  { name: "Steve", friend: true },
  { name: "Dan", friend: false },
  { name: "Bart", friend: true },
  { name: "Sarah", friend: false },
  { name: "Michelle", friend: false },
  { name: "Holly", friend: true }
];
trueFriends = peopleIknow.reduce((acc,i) => {
  i.friend && acc.push(i);// same as if(i.friend) then push
  return acc;
},[]);
console.log(trueFriends);

2 Comments

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
@Luca. When I answered, I just saw that there were 4 other answers just for a simple fix and with an explanation too.
0

You created filter and apply what the filter do in it, this code will work as you like.

var trueFriends = [];
var peopleIknow = [
      { name: "Steve", friend: true },
      { name: "Dan", friend: false },
      { name: "Bart", friend: true },
      { name: "Sarah", friend: false },
      { name: "Michelle", friend: false },
      { name: "Holly", friend: true }
    ];
    
   trueFriends = peopleIknow.filter(function(item){
      return item.friend});
    console.log(trueFriends)

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.