Arrow Function Help

const companies= [
  {name: "Company One", category: "Finance", start: 1981, end: 2004},
  {name: "Company Two", category: "Retail", start: 1992, end: 2008},
  {name: "Company Three", category: "Auto", start: 1999, end: 2007},
  {name: "Company Four", category: "Retail", start: 1989, end: 2010},
  {name: "Company Five", category: "Technology", start: 2009, end: 2014},
  {name: "Company Six", category: "Finance", start: 1987, end: 2010},
  {name: "Company Seven", category: "Auto", start: 1986, end: 1996},
  {name: "Company Eight", category: "Technology", start: 2011, end: 2016},
  {name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const retailCompanies = companies.filter(category => category==Retail);
console.log(retailCompanies);

I am struggling with arrow functions. what mistake I am committing?
This is the error →

image

Live Link here.

I would say that the mistake you’re making is not in regard to arrow functions, but is instead because Retail is not defined.

2 Likes

Hi there @Paul_wilkins

Thanks for replying. So what remedial correction would you suggest.

Define the Retail variable, that is currently not defined?

Oh I get it. You are wanting to compare against the category property of each company.

When you filter companies, each one is not a category. They are a company instead.
From that company you can get its category. That would result in the following code:

const retailCompanies = companies.filter(company => company.category === "Retail");
console.log(retailCompanies);

Thanks. I was faltering here.Sir, companies in an array what is the technical name for these entries:

Before the colon it’s called a key, after the colon it’s a value, and together they are called a property of the object, that you might also see referred to as a key/value pair.

1 Like

You might even be able to make use of destructuring, and use the following code instead:

We aren’t really all that interested in the company, but want the category property from it instead so we can use {category} as the function property.

const retailCompanies = companies.filter(({category}) => category === "Retail");
console.log(retailCompanies);

However, I find that arrow-notations become easier to understand when they are on a separate line:

const retailCompanies = companies.filter(
    ({category}) => category === "Retail"
);
console.log(retailCompanies);
1 Like

Sir, Thank you for sharing your experience and knowledge.
Please help me to understand this part as we are minimizing and polishing the code here → ({category})

I think this is something specific to JS or coding practice in general rather than filter or arrow function.

It’s a part of ES6 which took place in 2015. I find that the following place provides good details about destructuring, and other ES6 features.

http://es6-features.org/#ParameterContextMatching

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.