Trying to shorten redundant Javascript code

"duplicate code" refactoring
javascript function
how to remove duplicate block of code in java
javascript best practices 2019
javascript: best practice pdf
structuring javascript code
javascript good code examples
javascript class

This is a working javascript code. However, it looks redundant to me. Is there any way to clean this up?

let text = 'Some search text';

const searchMatch = 
entry.title.toLowerCase().includes(text.toLowerCase()) || 
entry.description.toLowerCase().includes(text.toLowerCase()) || 
entry.keywords.toLowerCase().includes(text.toLowerCase());

return searchMatch;

You might use an array and a .some test instead:

const textLower = text.toLowerCase();
return ['title', 'description', 'keywords']
  .map(prop => entry[prop].toLowerCase())
  .some(s => s.includes(textLower));

If, by chance, entry contains only those properties, then you could use Object.values instead:

return Object.values(entry)
  .map(s => s.toLowerCase())
  .some(s => s.includes(textLower));

JavaScript best practices, js file's greeting() function (the second one is ignored). In addition, attempting to declare the name variable a second time with the let keyword in  Stack Overflow Public questions and answers Teams Private questions and answers for your team Enterprise Private self-hosted questions and answers for your enterprise

You could do something like this:

const text = 'Some search text'.toLowerCase();

return [entry.title, entry.description, entry.keywords].some(s => s.toLowerCase().includes(text));

Functions, Paul Wilkins demonstrates various JavaScript refactoring techniques to make your In a recent thread on SitePoint's forums, some code was given to let one Try toggling the commented out lines and watch what happens. 35 videos Play all JavaScript Testing Udacity World's Most Famous Hacker Kevin Mitnick & KnowBe4's Stu Sjouwerman Opening Keynote - Duration: 36:30. Cyber Investing Summit Recommended for you

You could just use a one-line return statement involving an array composed of entry.description, entry.keywords and entry.title, and then using Array.prototype.some() to return a Boolean (true/false) value depending on whether any of the tests pass:

return [entry.description, entry.keywords, entry.title].some(string => string.toLowerCase().includes('Some search text'.toLowerCase());

Here's essentially a breakdown of each part:

[entry.description, entry.keywords, entry.title].some(...)

What this does is makes an anonymous array composed of entry.description, entry.keywords, and entry.title (the order does not matter) and iterates through it with the Array.prototype.some() method. According to the MDN page, .some():

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

Essentially iterates through each element, and depending on the callback from the provided function, and provides a Boolean value (true if at least one element in the array passes the test, false if no elements pass the test).

string => string.toLowerCase().includes('Some search text'.toLowerCase())

This is the anonymous function contained within the .some() method, and it takes a single parameter string. Then it returns a Boolean value, depending on the outcome of the .includes() method. The .includes() method returns another Boolean value, depending on whether the lowercased string contains the lowercased 'Some search text'. It's a mouthful, but in a nutshell, the line of code above reads:

If string in lowercased form includes 'Some search text' in lowercased form, return true - otherwise, return false.

Hopefully this helps you!

JavaScript Refactoring Techniques: Specific to Generic Code , Merging duplicate code simplifies the structure of your code and makes it shorter. Simplification + shortness = code that's easier to simplify and cheaper to support. I'm trying to shorten my script with a recuring code with variable input. The original code: Beek := " |----- |Claudio |----- |Bjorn GI|Bjorn G Mobile" TMClaudio := 12345 TCBjornG := 54321 TMBjornG := 67890 Gui, Font, s10 Gui, Add, Text, x8, Medewerkers Beek Gui, Font, s12, Arial Gui, Add, DropDownList, AltSubmit w200 vPosition1, %Beek% Gui, Add, Button, x8 y234 W80, Bellen Gui, Show, W216

Duplicate Code, a nonsystematically shortened code encoder having an input adapted to system for using fibre-channel :yclic-redundancy code (CRC) for data integrity in an 275,201 Int. Cl. G06F 11/00:11/10; H03M 13/00 J.S. CL 714— «07 20 Claims 1. JavaScript String: Exercise-16 with Solution. Write a JavaScript function to truncate a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("…") (by default) or specified characters. Test Data: console.log(text_truncate('We are doing JS string exercises.'))

Official Gazette of the United States Patent and Trademark Office: , Proper JavaScript performance is vital for ensuring that your website loads fast. performance issues and provide a list of best practices for optimizing JavaScript code. Removing unnecessary loops or calls within loops will speed up your You can also cut down on DOM traversal trips by storing pointer  I would like someone to look at my code and advice if there is any way to shorten it? Maybe another function that can be used? The macro copies cells from one worksheet ("macro") to the first empty row in another worksheet ("tracker"). For instance the cell L1 in "macro" needs to be copied to first empty row in column A in "tracker" etc.

20 Best Practices for Improving JavaScript Performance, JavaScript minification reduces unnecessary characters from scripts without Therefore, they can safely shorten variables and function names, further reducing characters. If there's an error in production, try to reproduce it in a development​  Try to keep your input validation part in the upper top of your functions, so that you won’t have to scroll down and juggle with lines when debugging. Try to refactor your code, understanding which condition depends on which other, and which condition is just independent from others.

Comments
  • one thing you can do text = text.toLowerCase()