Categories
JavaScript Answers

How to Sort an Object by Value in JavaScript?

Sometimes, we want to sort an object by value in JavaScript.

In this article, we’ll look at how to sort an object by value in JavaScript.

Sort an Object by Value in JavaScript

To sort an object by value in JavaScript, we can use the Object.values method to get the property values from the object in an array.

Then we can use the JavaScript sort method to sort the array values.

For instance, we can write:

const dict = {
  "x": 1,
  "y": 6,
  "z": 9,
  "a": 5,
  "b": 7,
  "c": 11,
  "d": 17,
  "t": 3
};

const items = Object.values(dict).sort((first, second) => {
  return second - first;
});

console.log(items)

We create the dict object with values we want to sort.

Then we call Object.values with dict to return an array of dict property values.

Next, we call sort with a callback that returns how we want to sort the items.

We subtract second by first to sort the property values in descending order.

Therefore, the console log logs:

[17, 11, 9, 7, 6, 5, 3, 1]

Conclusion

To sort an object by value in JavaScript, we can use the Object.values method to get the property values from the object in an array.

Then we can use the JavaScript sort method to sort the array values.

Categories
JavaScript Answers

What is the Equivalent of jQuery’s $(document).ready in Plain JavaScript?

Sometimes, we may want to run code when the DOM is loaded without using jQuery.

Therefore, we need to find an equivalent of jQuery’s $(document).ready in plain JavaScript.

In this article, we’ll look at how to find the equivalent of jQuery’s $(document).ready in plain JavaScript.

Equivalent of jQuery’s $(document).ready in Plain JavaScript

To find the equivalent of jQuery’s $(document).ready in plain JavaScript, we can listen to the DOMContentLoaded event on the document object with the addEventListener method.

To do this, we write:

document.addEventListener("DOMContentLoaded", (event) => {  
  console.log('document ready')  
});

to listen to the DOMContentLoaded event on document .

We call document.addEventListener with an event handler callback as the 2nd argument that runs when the DOM is successfully loaded.

Therefore, when the page is loaded, we should see 'document ready' logged.

Conclusion

To find the equivalent of jQuery’s $(document).ready in plain JavaScript, we can listen to the DOMContentLoaded event on the document object with the addEventListener method.

Categories
JavaScript Answers

How to Fix the “unexpected token o” Error When Trying to Parse JSON in Our JavaScript App?

Sometimes, we may run into the “unexpected token o” error when parsing JSON in our JavaScript app.

In this article, we’ll look at how to fix the “unexpected token o” error when trying to parse JSON in Our JavaScript app.

Fix the “unexpected token o” Error When Trying to Parse JSON in Our JavaScript App

To fix the “unexpected token o” error when trying to parse JSON in Our JavaScript app, we should make sure that we’re running JSON.parse on a valid JSON string.

For instance, instead of writing something like:

const obj = {  
  foo: 'bar'  
}  
console.log(JSON.parse(obj))

where we’re trying to call JSON.parse on the obj object, we should make sure that we’re calling JSON.parse on a valid JSON string.

For example, we write:

const obj = JSON.stringify({  
  foo: 'bar'  
})  
console.log(JSON.parse(obj))

to convert the object into a JSON string with JSON.stringify .

Then we can call JSON.parse successfully to parse the JSON string back into an object.

The console log should log:

{foo: "bar"}

Conclusion

To fix the “unexpected token o” error when trying to parse JSON in Our JavaScript app, we should make sure that we’re running JSON.parse on a valid JSON string.

Categories
JavaScript Answers

How to Fix the ‘Error: listen EADDRINUSE’ Error When Running a Node.js App?

Sometimes, we may run into the ‘Error: listen EADDRINUSE’ error when running a Node.js app.

In this article, we’ll look at how to fix the ‘Error: listen EADDRINUSE’ error when running a Node.js app.

Fix the ‘Error: listen EADDRINUSE’ Error When Running a Node.js App

To fix the ‘Error: listen EADDRINUSE’ error when running a Node.js app, we should make sure that no process is running on the same port that the Node.js app is running on.

For instance, if we have the following Express app code:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('hello world')
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Then we should make sure that no process is running on port 3000 before we run our Express app.

Conclusion

To fix the ‘Error: listen EADDRINUSE’ error when running a Node.js app, we should make sure that no process is running on the same port that the Node.js app is running on.

Categories
Express JavaScript Answers

How to Fix the “Can’t set headers after they are sent to the client” Error in an Express App?

Sometimes, we may run into the “Can’t set headers after they are sent to the client” when we run our Express app.

In this article, we’ll look at how to fix the “Can’t set headers after they are sent to the client” in our Express app.

Fix the “Can’t set headers after they are sent to the client” Error in an Express App

To fix the “Can’t set headers after they are sent to the client” in our Express app, we should make sure that we only send a response once in our Express middleware chain.

For instance, we shouldn’t have code like:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('hello world')
  res.send('hello world')
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

which calls res.send twice.

We should make sure that we only send a response once in our middleware chain.

This also means we shouldn’t have code like:

const express = require('express')
const app = express()
const port = 3000

app.use((req, res, next) => {
  res.send('hello world')
  next()
})

app.get('/', (req, res) => {
  res.send('hello world')
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

where we call res.send in our middleware that’s run before the / route handler.

And res.send is also called in the / route handler itself.

This will also cause the error.

Instead, we should write:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('hello world')
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

or:

const express = require('express')
const app = express()
const port = 3000
app.use((req, res, next) => {
  console.log('hello world')
  next()
})

app.get('/', (req, res) => {
  res.send('hello world')
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

where we only return a response once in the whole middleware chain.

Conclusion

To fix the “Can’t set headers after they are sent to the client” in our Express app, we should make sure that we only send a response once in our Express middleware chain.