Categories
JavaScript Answers

How to add a new key value pair in existing JSON object using JavaScript?

Sometimes, we want to add a new key value pair in existing JSON object using JavaScript.

In this article, we’ll look at how to add a new key value pair in existing JSON object using JavaScript.

How to add a new key value pair in existing JSON object using JavaScript?

To add a new key value pair in existing JSON object using JavaScript, we can parse the JSON string with JSON.parse, then add the key-value pairs we want, and then convert the object back to a JSON string with JSON.stringify.

For instance, we write:

const s = `{
  "workbookInformation": {
    "version": "9.1",
    "source-platform": "win"
  },
  "datasources1": {

  },
  "datasources2": {

  }
}`
const obj = JSON.parse(s)
obj.foo = 'bar'
const newS = JSON.stringify(obj)
console.log(newS)

to parse s into an object with JSON.parse.

Then add the foo property to obj.

And then we call JSON.stringify with obj to convert it back to a JSON string.

Therefore, newS is {"workbookInformation":{"version":"9.1","source-platform":"win"},"datasources1":{},"datasources2":{},"foo":"bar"}.

Conclusion

To add a new key value pair in existing JSON object using JavaScript, we can parse the JSON string with JSON.parse, then add the key-value pairs we want, and then convert the object back to a JSON string with JSON.stringify.

Categories
JavaScript Answers

How to erase previously drawn lines on an HTML5 canvas with JavaScript?

Sometimes, we want to erase previously drawn lines on an HTML5 canvas with JavaScript.

In this article, we’ll look at how to erase previously drawn lines on an HTML5 canvas with JavaScript.

How to erase previously drawn lines on an HTML5 canvas with JavaScript?

To erase previously drawn lines on an HTML5 canvas with JavaScript, we can overwrite the previous shape with a new image.

For instance, we write:

<canvas width='200' height='200'></canvas>

to add a canvas.

And we write:

const canvas = document.querySelector('canvas')
const context = canvas.getContext('2d');

context.fillStyle = 'blue';
context.fillRect(0, 0, 100, 100);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);

context.fillStyle = 'red';
context.fillRect(50, 50, 100, 100);

setTimeout(() => {
  context.putImageData(imageData, 0, 0);
}, 1000);

to draw rectangles on the canvas with fillRect.

We use the getImageData to get the canvas content before the red rectangle is drawn.

Then after we draw the red rectangle, we call putImageData with imageData to overwrite the blue and red rectangles with the canvas content that only has the blue rectangle.

Therefore, we see the blue and red rectangle drawn for a second, then we see the blue rectangle only thereafter.

Conclusion

To erase previously drawn lines on an HTML5 canvas with JavaScript, we can overwrite the previous shape with a new image.

Categories
JavaScript Answers

How to make a span element editable with JavaScript?

Sometimes, we want to make a span element editable with JavaScript.

In this article, we’ll look at how to make a span element editable with JavaScript.

How to make a span element editable with JavaScript?

To make a span element editable with JavaScript, we can set the contentEditable property of the span to true.

For instance, we write:

<span>hello world</span>
<button>
  edit
</button>

to add a span and a button.

Then we write:

const button = document.querySelector('button')
const span = document.querySelector('span')

button.onclick = () => {
  span.contentEditable = true
}

to make the span editable when we click on the button.

To do this, we select the span and button with querySelector.

Then we set button.onclick to a function that sets span.contentEditable to true.

As a result, when we click edit, then we can change the span’s text by typing in it.

Conclusion

To make a span element editable with JavaScript, we can set the contentEditable property of the span to true.

Categories
JavaScript Answers

How to convert array to string while preserving brackets with JavaScript?

Sometimes, we want to convert array to string while preserving brackets with JavaScript.

In this article, we’ll look at how to convert array to string while preserving brackets with JavaScript.

How to convert array to string while preserving brackets with JavaScript?

To convert array to string while preserving brackets with JavaScript, we can use the JSON.stringify method.

For instance, we write:

const a = [
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5]
];
const s = JSON.stringify(a);
console.log(s)

We call JSON.stringify with a to convert the array a to a string.

Therefore, s is '[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]'.

Conclusion

To convert array to string while preserving brackets with JavaScript, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to add a tooltip div with React and JavaScript?

Sometimes, we want to add a tooltip div with React and JavaScript.

In this article, we’ll look at how to add a tooltip div with React and JavaScript.

How to add a tooltip div with React and JavaScript?

To add a tooltip div with React and JavaScript, we can add event handlers for the mouseover and mouseout events.

For instance, we write:

import React from "react";

export default function App() {
  const [hover, setHover] = React.useState();
  const handleMouseIn = () => {
    setHover(true);
  };

  const handleMouseOut = () => {
    setHover(false);
  };

  const tooltipStyle = {
    display: hover ? "block" : "none"
  };

  return (
    <div>
      <div onMouseOver={handleMouseIn} onMouseOut={handleMouseOut}>
        hover
      </div>
      <div>
        <div style={tooltipStyle}>this is the tooltip!!</div>
      </div>
    </div>
  );
}

We define the hover state to keep track of when we hover over the ‘hover’ div.

Then we add the handleMouseIn and handleMouseOut functions to call setHover to true and false respwectively.

Then we add the mouseover and mouseout event handler to the div with `hover’ in it so that when we hover over it, the tooltip div will show.

We set onMouseOver to handleMouseIn and onMouseOut to handleMouseOut to add the handlers.

Next, we add the tooltip div by adding a div with the style prop set to tooltipStyle.

We set the display CSS property to 'block' if hover is true and false otherwise to show the tooltip when we hover over ‘hover’.

Conclusion

To add a tooltip div with React and JavaScript, we can add event handlers for the mouseover and mouseout events.