Tari Ibaba

Tari Ibaba is a software developer with years of experience building websites and apps. He has written extensively on a wide range of programming topics and has created dozens of apps and open-source libraries.

How to Check if a String Contains Numbers in JavaScript

To check if a string contains numbers in JavaScript, call the test() method on this regex: /\d/. test() will return true if the string contains numbers. Otherwise, it will return false.

For example:

function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

The RegExp test() method searches for a match between a regular expression and a string.

The / and / characters are used to start and end the regular expression.

The \d metacharacter matches any digit (09) in the string.

You can also use the [0-9] to match digits. This pattern matches any number character between 0 and 9.

function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

You might find [0-9] to be more readable than using \d, especially if you’re not very familiar with special characters in regular expressions.

Check if string contains only numbers

To check if the string contains only numbers, we’ll have to use a different regular expression – ^\d+$:

function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false

The ^ character marks the beginning of the string input, and the $ character marks the end of it.

Adding the + character after the \d makes regex match one or more occurrences of the \d pattern.

So the regex matches a string starting and ending with a consecutive sequence of digits.

As we did before, we could replace \d with [0-9] here:

function containsOnlyNumbers(str) {
  return /^[0-9]+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false

The String match() method

We can use the String match() method in place of RegExp test() to check if a string contains numbers

function containsNumbers(str) {
  return Boolean(str.match(/\d/));
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

The String match() method returns an array of all the matches of a regular expression in a string. If there are no matches, it returns null.

function containsNumbers(str) {
  return str.match(/\d/);
}

console.log(containsNumbers('hello123'));
// [ '1', index: 5, input: 'hello123', groups: undefined ]

console.log(containsNumbers('javascript')); // null

console.log(containsNumbers('3 apples'));
// [ '3', index: 0, input: '3 apples', groups: undefined ]

We pass the result of match() to the Boolean() constructor to convert it to a Boolean value. Boolean() converts truthy values to true, and falsy values to false.

In JavaScript, there are six falsy values: undefinednullNaN0'' (empty string), and false. Every other value is truthy.

console.log(Boolean(undefined)); // false
console.log(Boolean(['number60'])); // true
console.log(Boolean(null)); // false
console.log(Boolean(5)); // true

Your 10 Best JavaScript Books for Complete Mastery in 2023

As programmers, we’re always looking to improve. We want to write cleaner, more maintainable code, learn advanced language features for elegant and concise code, make our code easier for others to read and understand, and create more reusable and effective test cases.

If you’re looking to improve your skills, there are tons of resources available to help you out. From YouTube videos to seminars to answers on Stack Overflow, there’s no shortage of information out there. However, if you really want to delve deep into a particular subject, it’s hard to beat a good book. Specifically, books written for that specific field will provide a detailed explanation of all the important concepts, along with insights from experienced authors.

Check out these top JavaScript books to boost your skills! Whether you’re new to JavaScript or a seasoned pro, these books are comprehensive guides that can take you from beginner to expert. Plus, you can use them as a reference to brush up on concepts you may have forgotten or aren’t quite sure about in the future.

1. JavaScript: The Definitive Guide

Cover of JavaScript: The Definitive Guide

Author: David Flanagan

This has been a top-selling book for almost 25 years. The seventh edition is fully updated to cover the 2020 version of JavaScript (ES2020), including new chapters that cover classes, modules, iterators, generators, Promises, async/await, and metaprogramming. The book is meant for programmers interested in learning JavaScript and for web developers that want to improve their knowledge and understanding of the language.

Topics covered include: types, values, variables, expressions, operators, statements, objects, arrays, functions, classes, modules, iterators, generators, Promises, async/await, data structures, regular expressions, JSON, buffers, files, streams, and more.

2. Eloquent JavaScript: A Modern Introduction to Programming

Cover of Eloquent JavaScript: A Modern Introduction to Programming.

Author: Marijn Haverbeke

Newly updated only a few years back, Eloquent JavaScript gives you a closer look at essential JavaScript language features to help you quickly start writing beautiful and effective code.

It has been updated to reflect a more recent state of JavaScript and web browsers and includes brand-new material on features like class notation, arrow functions, iterators, async functions, template strings, and block scope.

A great read for beginners and experts alike, the book is packed with plenty of extensive examples that let you have a practical understanding of the concepts, as well as exercises and full-chapter projects that give you hands-on experience with writing your own programs.

Here are some of the things you’ll learn:

  • Essential elements of programming in general, like syntax, control flow, and data.
  • How to organize and clarify your code with object-oriented and functional programming language techniques.
  • How to write browser scripts and make basic web applications.
  • How to use the DOM effectively to interact with webpages.
  • How to build servers and utilities with Node.js.

3. A Smarter Way to Learn JavaScript

Cover of "A Smarter Way to Learn JavaScript"

Author: Mark Myers

If you’re new to programming, this will be a great book. It explains basic coding concepts with simple English and no assumptions about what you already know. If you’re an intermediate JavaScript developer, you will also benefit from the book, as it comes with a variety of exercises that will give you extra practice and sharpen your skills in any weak areas.

Topics covered include:

  • Fundamentals of JavaScript such as variables, math expressions, strings, if statements, arrays, loops, functions, etc.
  • Advanced JavaScript concepts such as constructors, prototypes, and exception handling.
  • DOM manipulation
  • Event handling
  • Brower control
  • Form validation

4. Head First JavaScript Programming

Cover of "Head First JavaScript Programming"

Authors: Eric Freeman, Elisabeth Robson

Head First is series known for its highly visual and interactive approach to teaching. This is a great read if you’re just getting started with JavaScript and want a less formal approach to learning the language that doesn’t use only text and all kinds of technical jargon. You’ll be playing games, solving puzzles, studying riddles, and interacting with JavaScript in many unconventional ways. You’ll also create a lot of real code so that you can start creating your own web applications.

It covers important topics such as: the inner details of JavaScript, how JavaScript works with the browser, data types in JavaScript, arrays, the power of functions, how to use objects, closures, writing and testing applications, etc.

5. You Don’t Know JS

Cover of "You Don't Know JS"

Author: Kyle Simpson

You Don’t Know JS is not a single book but is a collection of six well-written books for mastering JavaScript that can be read independently. Each book in the series gives a you thorough exploration of the core mechanisms of JavaScript to help you gain a deeper understanding of how the language works. The author takes a practical and thorough approach to explain the concepts in a way that will quickly bring you up to speed with the unique language features.

6. Effective JavaScript

Cover of "Effective JavaScript"

Author: David Herman

Effective JavaScript is organized around 68 proven ways to write better JavaScript, demonstrated with concrete examples. With this book, you’ll learn how to work effectively with flexible and expressive features of the language, and how to avoid pitfalls. No matter how long you’ve been writing JavaScript, Effective JavaScript will strengthen your understanding of this powerful language, so that you can build more predictable, reliable, and maintainable programs. Topics covered include: better ways to use prototype-based object-oriented programming, subtleties, and solutions for working with arrays and dictionary objects, precise and practical explanations of JavaScript functions and variable scoping semantics, etc.

7. JavaScript: The Good Parts

Cover of "JavaScript: The Good Parts"

Author: Douglas Crockford

Douglas Crockford is a highly renowned JavaScript expert in the development community, and in this book, he highlights the beautiful parts of the language that lets you create effective code. JavaScript was developed and released in a hurry before it could be refined, and this led to a lot of unrefined and error-prone features in the language. In JavaScript: The Good Parts, Crockford filters out the language blunders to offer you a detailed look at the genuinely elegant parts of the language in areas like syntax, objects, functions, inheritance, arrays, regular expressions, and methods.

8. JavaScript and jQuery: Interactive Front-End Web Development

Cover of "JavaScript and jQuery: Interactive Front-End Web Development"

Author: Jon Duckett

This book delivers a fully illustrated guide to making your websites more interactive and your interfaces more interesting and intuitive. You’ll explore basic programming language concepts that assume no prior knowledge of programming, beyond an ability to create a web page using HTML & CSS.

There is little reason to learn jQuery in 2022, but the book also teaches you how to use the library to simplify the process of writing scripts, after you’ve gained a solid understanding of JavaScript.

9. Professional JavaScript for Web Developers

Cover of "Professional JavaScript for Web Developers"

Author: Matt Frisbie

Professional JavaScript for Web Developers is a book written to help intermediate and advanced programmers improve the quality of their code. It looks into modern language features that will help you write cleaner code and become a more polished JavaScript developer. It explains fundamental web development concepts such as the document object model (DOM), the browser object model (BOM), events, forms, JSON, error handling, and web animation. It also covers advanced browser APIs such as geolocation, web workers, service workers and fetch. There are hundreds of working code examples that you can practice with to get familiar with the new concepts.

10. Secrets of the JavaScript Ninja

Cover of "Secrets of the JavaScript Ninja"

Authors: John Resig, Bear Bibeault, Josip Maras

In this book, you’ll master key JavaScript concepts such as functions, closures, objects, prototypes, and promises, using practical examples that clearly illustrate each concept and technique. You’ll discover best practice techniques like testing and cross-browser development, and look into regular expressions and asynchronous programming.

Bonus: JavaScript for Kids: A Playful Introduction to Programming

Cover of "JavaScript for Kids"

Author: Nick Morgan

JavaScript for Kids will be a gentle introduction to programming for aspiring developers. It teaches programming essentials through patient, step-by-step examples with lighthearted illustrations. The book has three parts; you’ll start with the basics like strings, conditional statements, and loops in part 1, then you’ll move on to more advanced topics like object-oriented programming, timers, and browser events in part 2. In part 3, you’ll learn how to use the HTML5 canvas to create drawings and animate them with JavaScript. After each part, you’ll write a cool game to gain a practical understanding of the lessons covered.

Conclusion

You can always improve your skills, no matter how long you’ve been programming. Whether you’re just getting started with JavaScript or you’re a seasoned expert, the knowledge from these books will refresh your memory on various language features, and help you write cleaner and more polished code.

This post contains affiliate links. We may earn a commission if you make a purchase through them, at no additional cost to you.

How to Subtract Seconds From a Date in JavaScript

Let’s learn how to easily subtract any number of seconds from a Date object in JavaScript.

1. Date setSeconds() and getSeconds() methods

To subtract seconds from a Date:

  1. Call the getSeconds() method on the Date to get the number of seconds.
  2. Subtract the seconds.
  3. Pass the result of the subtraction to the setSeconds() method.

For example:

function subtractSeconds(date, seconds) {
  date.setSeconds(date.getSeconds() - seconds);

  return date;
}

// July 10, 2022 at 5:35:40 pm
const date = new Date('2022-07-10T17:35:40.000Z');

const newDate = subtractSeconds(date, 10);

// July 10, 2022 at 5:35:30 pm
console.log(newDate); // 2022-07-10T17:35:30.000Z

Our subtractSeconds() function takes a Date object and the number of seconds to subtract as arguments. It returns the same Date object with the seconds subtracted.

The Date getSeconds() method returns a number between 0 and 59 that represents the seconds of a particular Date.

The Date setSeconds() method sets the seconds of a Date to a specified number.

If the seconds subtracted decrease the minute, hour, day, month, or year of the Date object, setHours() automatically updates the Date information to reflect this.

// July 10, 2022 at 5:20:00 pm
const date = new Date('2022-07-10T17:20:00.000Z');

date.setSeconds(date.getSeconds() - 130);

// July 10, 2022 at 5:17:50 pm
console.log(date); // 2022-07-10T17:17:50.000Z

In this example, decreasing the seconds of the Date by 130 decreases the minutes by 3 and sets the seconds to 50.

Avoiding side effects

The setSeconds() method mutates the Date object it is called on. This introduces a side effect into our subtractSeconds() function. To avoid modifying the passed, and create a pure function, make a copy of the Date and call setSeconds() on this copy, instead of the original.

function subtractSeconds(date, seconds) {
  // make copy with Date() constructor
  const dateCopy = new Date(date);

  dateCopy.setSeconds(date.getSeconds() - seconds);

  return dateCopy;
}

// July 10, 2022 at 5:35:40 pm
const date = new Date('2022-07-10T17:35:40.000Z');

const newDate = subtractSeconds(date, 10);

// July 10, 2022 at 5:35:30 pm
console.log(newDate); // 2022-07-10T17:35:30.000Z

// original not modified
console.log(date); // 2022-07-10T17:35:40.000Z

Tip: Functions that don’t modify external state (i.e., pure functions) tend to be more predictable and easier to reason about, as they always give the same output for a particular input. This makes it a good practice to limit the number of side effects in your code.

2. date-fns subSeconds() function

Alternatively, we can use the subSeconds() function from the date-fns NPM package to quickly subtract seconds from a Date. It works similarly to our pure subtractSeconds() function.

import { subSeconds } from 'date-fns';

// July 10, 2022 at 5:35:40 pm
const date = new Date('2022-07-10T17:35:40.000Z');

const newDate = subSeconds(date, 10);

// July 10, 2022 at 5:35:30 pm
console.log(newDate); // 2022-07-10T17:35:30.000Z

// original not modified
console.log(date); // 2022-07-10T17:35:40.000Z

How to Use a Button as a Vue Router Link

To use a button as a Vue Router link, wrap the button element in a router-link component. When the button is clicked, the browser will navigate to the specified route without refreshing the page.

<template>
  <div>
    <router-link
      to="/posts"
      custom
      v-slot="{ navigate }"
    >
      <button
        @click="navigate"
        role="link"
      >
        Posts
      </button>
    </router-link>
  </div>
</template>
Navigating to the "/posts" route without refreshing the page.
Navigating the the /posts route without refreshing the page.

The router-link component has a default slot with a navigate function property.

The to prop is used to set the route that the browser should navigate to when this navigate function is called.

We make navigate an event handler of the button click event, so it is called when the button is clicked, making the browser navigate to the route specified with the to prop.

Setting the custom prop to true tells the router-link not to wrap its content in an <a> element, and allow us to create a custom router link.

The above method only works for Vue 3.x though. If working with Vue 2.x, you can use a button as a router link by setting the tag prop to the name of the button component, and setting the to prop to the specific route.

<template>
  <div>
    <router-link to="/posts" tag="button">Posts</router-link>
  </div>
</template>

The $router.push() method

Alternatively, we can use a button as a Vue router link by calling the push() method on the $router variable made available by Vue Router.

<template>
  <div>
    <button @click="$router.push('/posts')">Posts</button>
  </div>
</template>

The $router variable represents the router instance and can be used for programmatic navigation.

When using Vue Router with Vuetify, we can use the button component as a router link by setting the v-btn to prop.

<template>
  <div>
    <v-btn
      to="/posts"
      color="primary"
      dark
    >
      Posts
    </v-btn>
  </div>
</template>

We can set the b-button to prop to use the Vue Bootstrap button component as a Vue Router link.

<template>
  <div>
    <b-button
      to="/posts"
      variant="success"
    >
      Posts
    </b-button>
  </div>
</template>

How to Use a Button as a Link in Vue

To use a button as a link in Vue, you can wrap the button in an anchor (<a>) element. Clicking a link button makes the browser navigate to the specified URL.

<template>
  <div>
    <a href="/posts">
      <button>Posts</button>
    </a>
  </div>
</template>
Clicking the link button to navigate to another page.
Clicking the link button to navigate to another page.

When working with Vue Router, you can make a button serve as a router link using the navigate function provided by the router-link default slot.

<template>
  <div>
    <router-link
      to="/posts"
      custom
      v-slot="{ navigate }"
    >
      <button
        @click="navigate"
        role="link"
      >
        Posts
      </button>
    </router-link>
  </div>
</template>
Navigating to the "/posts" route without refreshing the page.
Navigating to the /posts route without refreshing the page.

We use navigate as a handler for the click event, so it is called when the button is clicked, making the browser navigate to the specified route without refreshing the page.

The above method only works for Vue 3.x though. If working with Vue 2.x, you can use a button as a router link by setting the tag prop to the name of the button component, and setting the to prop to the specific route.

<template>
  <div>
    <router-link to="/posts" tag="button">Posts</router-link>
  </div>
</template>

The $router.push() method

Alternatively, we can use a button as a Vue router link by calling the push() method on the $router variable made available by Vue Router.

<template>
  <div>
    <button @click="$router.push('/posts')">Posts</button>
  </div>
</template>

The $router variable represents the router instance, and can be used for programmatic navigation.

When working with the Vuetify framework, we can use the button component as a link by setting the v-btn href prop.

<template>
  <div>
    <v-btn
      href="/posts"
      color="primary"
      dark
    >
      Posts
    </v-btn>
  </div>
</template>

When using Vue Router with Vuetify, we can use the button component as a router link by setting the v-btn to prop.

<template>
  <div>
    <v-btn
      to="/posts"
      color="primary"
      dark
    >
      Posts
    </v-btn>
  </div>
</template>

If you’re working with the Vue Bootstrap framework, you can use the href to turn the button component into a link.

<template>
  <div>
    <b-button
      href="/posts"
      variant="primary"
    >
      Posts
    </b-button>
  </div>
</template>

We can set the b-button to prop to use the Vue Bootstrap button component as a Vue Router link.

<template>
  <div>
    <b-button
      to="/posts"
      variant="success"
    >
      Posts
    </b-button>
  </div>
</template>

How to Fix the “Cannot redeclare block-scoped variable” Error in TypeScript

Are you experiencing the “Cannot redeclare block-scoped variable” error in TypeScript? This error can occur for two reasons:

  1. Using variable names that clash with TypeScript global typings.
  2. Redeclaring a variable in the same block scope.
The "cannot redeclare block-scoped variable" TypeScript error occuring in VS Code.
The “cannot redeclare block-scoped variable” error occurring in VS Code.

We’ll look at solutions for these possible causes in this article.

Fix for: using variable names that clash with TypeScript global typings

The “Cannot redeclare block-scoped variable” error occurs if you declare a variable with a name that clashes with one declared in TypeScript global typings.

index.ts

// ❌ Cannot re-declare block-scoped variable "name".
const name = 'Coding Beauty';

console.log(name);

To fix the error in this case, convert your file to an ES module, like this:

index.ts

// ✅ variable declared successfully
const name = 'Coding Beauty';

console.log(name); // Coding Beauty

export {};

The export {} statement indicates that the file is an ES module. In TypeScript, any file containing a top-level import or export is considered to be a module.

Without top-level import or export declarations, the file gets treated as a script whose contents are available in the global scope (and to other modules). This is what causes the name clash between our name variable and the name variable declared in TypeScript global typings.

Another way to fix this is to use another name to declare the variable that does not clash with the global typings.

index.ts

// ✅ variable declared successfully
const theName = 'Coding Beauty';

console.log(theName); // Coding Beauty

export {};

Fix for: redeclaring a variable in the same block scope

The “cannot redeclare block-scoped variable” error will also occur if you try to declare a variable using a name previously used in the same block scope.

const language = 'JavaScript';

// ❌ Cannot redeclare block-scoped variable 'language'.
const language = 'PHP';

You can easily fix the error in the case by using a different name for the new variable.

const language = 'JavaScript';

// ✅ variable declared successfully
const language2 = 'PHP';

If you intended to assign a new value to the variable, the proper way to do this is to declare the variable with the let keyword, and change its value without redeclaring it.

// declare with "let" keyword
let language = 'JavaScript';

// reassign without redeclaring
language = 'PHP';

console.log(language); // PHP

Note

Unlike const or let, the var keyword doesn’t complain about redeclared variables.

var language = 'JavaScript';

// No error thrown
var language = 'PHP';

Redeclaring variables can cause tricky bugs in your code, and this is one reason to avoid using the var keyword.

You can declare a variable with the same name in a nested block. The variable in the nested block is separate from the one declared in the outer scope.

let color = 'red';

if (true) {
  let color = 'yellow';

  console.log(color); // yellow
}

console.log(color); // red

Note

If you use var keyword to do this, it will override the value of the variable in the outer scope.

var color = 'red';

if (true) {
  var color = 'yellow';

  console.log(color); // yellow
}

console.log(color); // yellow

Another reason to avoid using var.

It doesn’t have to be an if block, we can do this in any nested block designated with curly braces ({ and }).

let color = 'red';

{
  let color = 'yellow';

  console.log(color); // yellow
}

console.log(color); // red

Fix: use an IIFE

Another way to fix the error is to wrap the code containing the variable with an immediately invoked function expression (IIFE). IIFEs are functions that run as soon as they are defined, and they can help to avoid name clashes that cause this error.

const fruit = 'Apple';

(() => {
  const fruit = 'Banana';

  // ✅ variable declared successfully
  console.log(fruit); // Banana
})();

console.log(fruit); // Apple

This fix also solves the issue of TypeScript global typing clashes we looked at earlier.

index.ts

(() => {
  const name = 'Coding Beauty';

  console.log(name); // Coding Beauty
})();

How to Convert JSON to CSV in JavaScript

You can use the json2csv library to quickly convert JSON to CSV in JavaScript. It is a robust utility that converts JSON to CSV with column titles and proper line endings, and is available as an NPM package.

import { parse } from 'json2csv';

const obj = [
  { firstName: 'Russell', lastName: 'Castillo', age: 23 },
  { firstName: 'Christy', lastName: 'Harper', age: 35 },
  { firstName: 'Eleanor', lastName: 'Mark', age: 26 },
];

const csv = parse(obj);

console.log(csv);

This code will have the following output:

"firstName","lastName","age"
"Russell","Castillo",23
"Christy","Harper",35
"Eleanor","Mark",26

What are JSON and CSV?

Let’s quickly go through these terms in case you’re not familiar with them.

JSON (JavaScript Object Notation) is a text format used to store and transport data based on JavaScript object syntax, and is commonly used to build RESTful APIs.

CSV (Comma-Separated Values) is a method of storing tabular data as text using comma-separated values (hence the name). Each record is stored as a new line, and each field in the record is separated with a character that’s usually a , (comma).

Install json2csv

Before using json2csv, we’ll need to install it in our project. We can do with the NPM or Yarn CLI.

npm i json2csv

# Yarn
yarn add json2csv

After installation, we’ll be able to import it into a JavaScript module, like this:

import { parse } from 'json2csv';

// CommonJS module
const { parse } = require('json2csv');

The parse() function

The parse() function is a convenience method provided by json2csv, and it has two parameters. The first is the JSON string to convert to CSV, and the second is an object.

const csv = parse(obj, { delimiter: ' ', header: false });

[Update] json2csv is now legacy

The json2csv library has now been split into smaller libraries that are now published to NPM independently. json2csv is still available on NPM, but the newer way to use its functionality is through the @json2csv/plainjs module.

So instead of this:

import { parse } from 'json2csv';

// ...

const csv = parse(obj, { delimiter: '|' });

You’ll install @json2csv/plainjs and write this:

import { Parser } from '@json2csv/plainjs';

// ...

const parser = new Parser({ delimiter: '|' });

const csv = parser.parse(obj);


Customize conversion of JSON to CSV

The object specifies various options that customize the conversion.

One such option is delimeter, which indicates the character used to separate the columns in the CSV string. A comma is used if delimeter is not set.

For example, this:

import { parse } from 'json2csv';

const obj = [
  { firstName: 'Russell', lastName: 'Castillo', age: 23 },
  { firstName: 'Christy', lastName: 'Harper', age: 35 },
  { firstName: 'Eleanor', lastName: 'Mark', age: 26 },
];

const csv = parse(obj, { delimiter: '|' });

console.log(csv);

produces the following CSV string:

"firstName"|"lastName"|"age"
"Russell"|"Castillo"|23
"Christy"|"Harper"|35
"Eleanor"|"Mark"|26

We also have header, which determines whether or not the CSV string will contain a title row. It’s true by default.

const csv = parse(obj, { delimiter: '|', header: false });

CSV output:

"Russell"|"Castillo"|23
"Christy"|"Harper"|35
"Eleanor"|"Mark"|26

There’s also the quote option, which sets the quote used around column names. It’s " (double quote) by default, we can remove quotes by passing an ('') empty string to quote:

const csv = parse(obj, { quote: '' });
firstName,lastName,age
Russell,Castillo,23
Christy,Harper,35
Eleanor,Mark,26

Native conversion of JSON to CSV in JavaScript

Here’s how we can easily convert JSON to CSV in JavaScript:

function jsonToCsv(items) {
  const header = Object.keys(items[0]);

  const headerString = header.join(',');

  // handle null or undefined values here
  const replacer = (key, value) => value ?? '';

  const rowItems = items.map((row) =>
    header
      .map((fieldName) => JSON.stringify(row[fieldName], replacer))
      .join(',')
  );

  // join header and body, and break into separate lines
  const csv = [headerString, ...rowItems].join('\r\n');

  return csv;
}

const obj = [
  { color: 'red', maxSpeed: 120, age: 2 },
  { color: 'blue', maxSpeed: 100, age: 3 },
  { color: 'green', maxSpeed: 130, age: 2 },
];

const csv = jsonToCsv(obj);

console.log(csv);

This will be the CSV output:

color,maxSpeed,age
"red",120,2
"blue",100,3
"green",130,2

How it works

We created a reusable jsonToCsv() function to let us convert multiple JSON strings to CSV. It takes an array that contains objects. Each object will take up one row in the CSV output.

The first we do in this function is to get all the keys that will be used for the CSV header. We expect all objects in the array to have the same keys, so we use the Object.keys() method to extract the keys from the first object item into an array.

const obj = [
  { color: 'red', maxSpeed: 120, age: 2 },
  { color: 'blue', maxSpeed: 100, age: 3 },
  { color: 'green', maxSpeed: 130, age: 2 },
];

// { color: 'red', maxSpeed: 120, age: 2 }
console.log(obj[0]);

// [ 'color', 'maxSpeed', 'age' ]
console.log(Object.keys(obj[0]));

After getting the keys, we call the join() method on the array to concatenate all the elements into a CSV header string.

const header = ['color', 'maxSpeed', 'age'];

const headerString = arr.join(',');

console.log(headerString); // color,maxSpeed,age

Next, we create a function that will be passed as a callback to the replacer parameter of the JSON.stringify() function. This function will handle undefined or null property values of the objects in the JSON array.

const obj = { prop1: 'Earth', prop2: undefined };

// replace undefined property values with empty string ('')
const replacer = (key, value) => value ?? '';

const str = JSON.stringify(obj, replacer);

// {"prop1":"Earth","prop2":""}
console.log(str);

We then use the Array map() method to get the property values from each object. map() takes a callback function that is called on each array element to return a transformation.

This callback uses the header array to get all the keys of each object. With another call to map(), it goes through each key, gets the corresponding value for that key in the object, and converts it to a string using JSON.stringify().

This inner call to map() eventually results in an array of all the stringified property values of the current object in the array.

const header = ['color', 'maxSpeed', 'age'];

const row = { color: 'red', maxSpeed: 120, age: 2 };

const replacer = (key, value) => value ?? '';

const rowItem = header.map((fieldName) =>
  JSON.stringify(row[fieldName], replacer)
);

// array of stringified property values
console.log(rowItem); // [ '"red"', '120', '2' ]

After the object has been transformed into a property value array, join() is then used to convert the array to a CSV row.

['"red"', '120', '2'].join(',') // -> "red",120,2

So this transformation happens for every object in the JSON array to generate a list of CSV rows, stored in the rowItems variable in our original example.

To generate the final CSV output, we combine the headerString and the rowItems into one array, making use of the spread syntax (...).

const headerString = ['color', 'maxSpeed', 'age'];

const rowItems = ['"red",120,2', '"blue",100,3', '"green",130,2'];

[headerString, ...rowItems];
/*
Output ->
[
  [ 'color', 'maxSpeed', 'age' ],
  '"red",120,2',
  '"blue",100,3',
  '"green",130,2'
]
 */

Then we call join() on this array with the '\r\n' string as a separator, to create a string with the CSV header and each CSV row in a separate line.

const headerString = ['color', 'maxSpeed', 'age'];

const rowItems = ['"red",120,2', '"blue",100,3', '"green",130,2'];

console.log([headerString, ...rowItems].join('\r\n'));
/*
color,maxSpeed,age
"red",120,2
"blue",100,3
"green",130,2
 */

How to Add an Element to an Array in Vue

Add Element to Array With push() Method

To add an element to an array in Vue, call the push() method in the array with the element as an argument. The push() method will add the element to the end of the array.

For example:

<template>
  <div id="app">
    <button @click="addFruit">Add fruit</button>
    <ul>
      <h2
        v-for="(fruit, i) in fruits"
        :key="i"
      >
        {{ fruit }}
      </h2>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruits: ['Orange', 'Apple'],
    };
  },
  methods: {
    addFruit() {
      this.fruits.push('Banana');
    },
  },
};
</script>
Clicking the button adds a new fruit element.
Clicking the button adds a new fruit element.

The Array push() method adds one or more elements to the end of an array and returns the length of the array.

We use the v-for Vue directive to display the elements in the array. These rendered elements are automatically updated in the view when the array is modified with push().

Add Object Element to Array in Vue

We can use the same approach to add an object to an array and display more complex data. We just have to make sure that we render the properties of each object in the array, not the object itself.

<template>
  <div id="app">
    <button @click="addFruit">Add fruit</button>
    <ul>
      <h2
        v-for="(fruit, i) in fruits"
        :key="i"
      >
        <!-- Render "name" and "amount" properties -->
        {{ fruit.name }} ({{ fruit.amount }})
      </h2>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruits: [
        { name: 'Orange', amount: 3 },
        { name: 'Apple', amount: 5 },
      ],
    };
  },
  methods: {
    addFruit() {
      // Add object element to array
      this.fruits.push({ name: 'Banana', amount: 4 });
    },
  },
};
</script>
Clicking the button adds a new fruit item with a certain amount.
Clicking the button adds a new fruit element with a certain amount.

Like before, the list automatically updates in the view when a new object element is added to the array.

How to Add an Item to an Array in Vue

Add Item to Array With push() Method

To add an item to an array in Vue, call the push() method in the array with the item as an argument. The push() method will add the item to the end of the array.

For example:

<template>
  <div id="app">
    <button @click="addFruit">Add fruit</button>
    <ul>
      <h2
        v-for="(fruit, i) in fruits"
        :key="i"
      >
        {{ fruit }}
      </h2>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruits: ['Orange', 'Apple'],
    };
  },
  methods: {
    addFruit() {
      this.fruits.push('Banana');
    },
  },
};
</script>
Clicking the button adds a new fruit item.
Clicking the button adds a new fruit item.

The Array push() method adds one or more items to the end of an array and returns the length of the array.

We use the v-for Vue directive to display the items in the array. These rendered items are automatically updated in the view when the array is modified with push().

Add Object Item to Array in Vue

We can use the same approach to add an object to an array and display more complex data. We just have to make sure that we render the properties of each object in the array, not the object itself.

<template>
  <div id="app">
    <button @click="addFruit">Add fruit</button>
    <ul>
      <h2
        v-for="(fruit, i) in fruits"
        :key="i"
      >
        <!-- Render "name" and "amount" properties -->
        {{ fruit.name }} ({{ fruit.amount }})
      </h2>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruits: [
        { name: 'Orange', amount: 3 },
        { name: 'Apple', amount: 5 },
      ],
    };
  },
  methods: {
    addFruit() {
      // Add object item to array
      this.fruits.push({ name: 'Banana', amount: 4 });
    },
  },
};
</script>
Clicking the button adds a new fruit item with a certain amount.
Clicking the button adds a new fruit item with a certain amount.

Like before, the list automatically updates in the view when a new object item is added to the array.

How to Subtract Hours From a Date in JavaScript

Let’s learn how to easily subtract any number of hours from a Date object in JavaScript.

1. Date setHours and getHours() Methods

To subtract hours from a Date:

  1. Call the getHours() method on the Date to get the number of hours.
  2. Subtract the hours.
  3. Pass the result of the subtraction to the setHours() method.
function subtractHours(date, hours) {
  date.setHours(date.getHours() - hours);

  return date;
}

// 8 AM on June 20, 2022
const date = new Date('2022-06-20T08:00:00.000Z');

const newDate = subtractHours(date, 2);

// 6 AM on June 20, 2022
console.log(date); // 2022-06-20T06:00:00.000Z

Our subtractHours() function takes a Date object and the number of hours to subtract as arguments. It returns the same Date object with the hours subtracted.

The Date getHours() method returns a number between 0 and 23 that represents the hours of a particular Date.

The Date setHours() method sets the hours of a Date to a specified number.

If the hours we subtract would decrease the day, month, or year of the Date, setHours() automatically updates the Date information to reflect this.

// 12 AM on June 20, 2022
const date = new Date('2022-06-20T00:00:00.000Z');

date.setHours(date.getHours() - 3);

// 9 PM on June 19, 2022 (previous day)
console.log(date); // 2022-06-19T21:00:00.000Z

In this example, decreasing the hours of the Date by 3 decreases the day by 1 and sets the hours to 21.

Avoiding Side Effects

The setHours() method mutates the Date object it is called on. This introduces a side effect into our subtractHours() function. To avoid modifying the passed date and create a pure function, make a copy of the date and call setHours() on this copy, instead of the original:

function subtractHours(date, hours) {
  const dateCopy = new Date(date);

  dateCopy.setHours(dateCopy.getHours() - hours);

  return date;
}

// 8 AM on June 20, 2022
const date = new Date('2022-06-20T08:00:00.000Z');

const newDate = subtractHours(date, 2);

// 6 AM on June 20, 2022
console.log(date); // 2022-06-20T06:00:00.000Z

// Original not modified
console.log(newDate); // 2022-06-20T08:00:00.000Z

Tip: Functions that don’t modify external state (i.e., pure functions) tend to be more predictable and easier to reason about. This makes it a good practice to limit the number of side effects in your code.

2. date-fns subHours() Function

Alternatively, we can use the subHours() function from the date-fns NPM package to quickly subtract hours from a Date. It works similarly to our pure subtractHours() function.

import { subHours } from 'date-fns';

// 8 AM on June 20, 2022
const date = new Date('2022-06-20T08:00:00.000Z');

const newDate = subHours(date, 2);

// 6 AM on June 20, 2022
console.log(date); // 2022-06-20T06:00:00.000Z

// Original not modified
console.log(newDate); // 2022-06-20T08:00:00.000Z