Categories
JavaScript Answers

How to filter a complex JSON object using JavaScript?

Sometimes, we want to filter a complex JSON object using JavaScript.

In this article, we’ll look at how to filter a complex JSON object using JavaScript.

How to filter a complex JSON object using JavaScript?

To filter a complex JSON object using JavaScript, we can use the array filter method.

For instance, we write:

const obj = {
  "Lofts": "none",
  "Maisons": "2",
  "homes": [{
    "home_id": "1",
    "price": "925",
    "num_of_beds": "2"
  }, {
    "home_id": "2",
    "price": "1425",
    "num_of_beds": "4",
  }, {
    "home_id": "3",
    "price": "333",
    "num_of_beds": "5",
  }]
};

const filtered = obj.homes.filter((a) => {
  return a.home_id === '2';
});
console.log(filtered)

to call obj.homes.filter with a callback to return an array with the object with home_id property equal to 2.

As a result, filtered is

[{
  home_id: "2",
  num_of_beds: "4",
  price: "1425"
}]

Conclusion

To filter a complex JSON object using JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to fix className styles not working in React and JavaScript?

Sometimes, we want to fix className styles not working in React and JavaScript.

In this article, we’ll look at how to fix className styles not working in React and JavaScript.

How to fix className styles not working in React and JavaScript?

To fix className styles not working in React and JavaScript, we can import the styles file as a module.

For instance, we write:

styles.module.scss
.app {
  font-family: sans-serif;
  text-align: center;
}

to add some classes with styles into a styles file.

Then in the same folder, we write:

App.js
import React from "react";
import styles from "./styles.module.scss";

export default function App() {
  return <div className={styles.app}>hello world</div>;
}

to import the styles module as a default export.

Next, we reference the app class in styles.module.scss with styles.app.

Conclusion

To fix className styles not working in React and JavaScript, we can import the styles file as a module.

Categories
JavaScript Answers

How to get promise value in React and JavaScript?

Sometimes, we want to get promise value in React and JavaScript.

In this article, we’ll look at how to get promise value in React and JavaScript.

How to get promise value in React and JavaScript?

To get promise value in React and JavaScript, we can use await.

For instance, we write:

import React from "react";

export default function App() {
  const [ans, setAns] = React.useState();

  const getAnswer = async () => {
    const res = await fetch("https://yesno.wtf/api");
    const data = await res.json();
    setAns(data);
  };

  React.useEffect(() => {
    getAnswer();
  }, []);

  return <>{JSON.stringify(ans)}</>;
}

to create the getAnswer function that calls fetch with await to get the response data from the promise returned by fetch.

Likewise, we do the same with the json method.

And then we call setAns to set the value of ans.

Next, we call getAnswer in the useEffect callback to call when the component mounts.

And finally, we render ans in the return statement.

Conclusion

To get promise value in React and JavaScript, we can use await.

Categories
JavaScript Answers

How to pass multiple arguments into a JavaScript callback function?

Sometimes, we want to pass multiple arguments into a JavaScript callback function.

In this article, we’ll look at how to pass multiple arguments into a JavaScript callback function.

How to pass multiple arguments into a JavaScript callback function?

To pass multiple arguments into a JavaScript callback function, we can use the spread operator.

For instance, we write:

const doSomething = (...args) => {
  console.log(args)
}

class SomeClass {
  constructor(callback, params) {
    this.callback = callback;
    this.params = params
  }

  method() {
    this.callback(...this.params)
  }
}

const a = ['v1text', 'v2text']
const obj = new SomeClass(doSomething, a);
obj.method()

to create the someClass class that takes a callback function and params parameters.

We set them both as values for properties of this.

And we define the method method that calls this.callback with this.params spread as arguments.

Next, we create a SomeClass instance with

const obj = new SomeClass(doSomething, a);

And then we call obj.method to call doSomething with a array.

Therefore, we see ["v1text", "v2text"] logged from doSomething.

Conclusion

To pass multiple arguments into a JavaScript callback function, we can use the spread operator.

Categories
JavaScript Answers

How to catch the moment the input box lost focus with JavaScript?

Sometimes, we want to catch the moment the input box lost focus with JavaScript.

In this article, we’ll look at how to catch the moment the input box lost focus with JavaScript.

How to catch the moment the input box lost focus with JavaScript?

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onblur = () => {
  console.log('blurred')
}

to select the input with querySelector.

And then we set input.onblur to a function that logs 'blurred'.

Therefore, when we move focus away from the input, we see 'blurred' logged.

Conclusion

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.