Categories
JavaScript Answers

How to copy JavaScript object to new variable not by reference?

Sometimes, we want to copy JavaScript object to new variable not by reference.

In this article, we’ll look at how to copy JavaScript object to new variable not by reference.

How to copy JavaScript object to new variable not by reference?

To copy JavaScript object to new variable not by reference, we make a copy of the object before we pass it into a function as an argument.

For instance, we write

const copyObj = JSON.parse(JSON.stringify(jsonObj));

to call JSON.stringify with jsonObj and then call JSON.parse on the JSON string to make a deep copy of the object.

And then we assign the returned copied object to copyObj.

Then we can use copyObj as the argument of a function and it doesn’t reference the original jsonObj object.

Conclusion

To copy JavaScript object to new variable not by reference, we make a copy of the object before we pass it into a function as an argument.

Categories
JavaScript Answers

How to convert a binary Node.js Buffer to JavaScript ArrayBuffer?

Sometimes, we want to convert a binary Node.js Buffer to JavaScript ArrayBuffer.

In this article, we’ll look at how to convert a binary Node.js Buffer to JavaScript ArrayBuffer.

How to convert a binary Node.js Buffer to JavaScript ArrayBuffer?

To convert a binary Node.js Buffer to JavaScript ArrayBuffer, we can use the Uint8Array constructor.

For instance, we write

const arrayBuffer = new Uint8Array(nodeBuffer).buffer;

to create the Uint8Array from the nodeBuffer Node.js buffer object.

Then we get the array buffer from the buffer property.

Conclusion

To convert a binary Node.js Buffer to JavaScript ArrayBuffer, we can use the Uint8Array constructor.

Categories
JavaScript Answers

How to split string and keep the separator with JavaScript and regex?

Sometimes, we want to split string and keep the separator with JavaScript and regex.

In this article, we’ll look at how to split string and keep the separator with JavaScript and regex.

How to split string and keep the separator with JavaScript and regex?

To split string and keep the separator with JavaScript and regex, we can call string’s split method with a regex.

For instance, we write

const a = "1、2、3".split(/(、)/g) 

to call split with /(、)/g to return an iterable object with all the values around the delimier in addition to the delimiters themselves.

Therefore, we get returned.

["1", "、", "2", "、", "3"]

Conclusion

To split string and keep the separator with JavaScript and regex, we can call string’s split method with a regex.

Categories
JavaScript Answers

How to create a JavaScript ES6 promise for loop?

Sometimes, we want to create a JavaScript ES6 promise for loop.

In this article, we’ll look at how to create a JavaScript ES6 promise for loop.

How to create a JavaScript ES6 promise for loop?

To create a JavaScript ES6 promise for loop, we use a for-of loop.

For instance, we write

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

(async function loop() {
  for (const d of delays) {
    await delay(d);
  }
})();

to create a loop function that runs a for-of loop to loop through the delays array.

In the loop, we call delay with d in delays which returns a promise.

And we wait for delay to finish running with await in each iteration.

Conclusion

To create a JavaScript ES6 promise for loop, we use a for-of loop.

Categories
React Native Answers

How to hide or show components in React Native?

Sometimes, we want to hide or show components in React Native.

In this article, we’ll look at how to hide or show components in React Native.

How to hide or show components in React Native?

To hide or show components in React Native, we can use the && operator.

For instance, we write

{
  showInput && <TextInput />;
}

to show the TextInput only when showInput is true.

showInput can be a state or a prop.

When a state or prop changes, the component will re-render.

Conclusion

To hide or show components in React Native, we can use the && operator.