Categories
JavaScript Answers

How to alter and assign JavaScript object properties without side effects?

Sometimes, we want to alter and assign JavaScript object properties without side effects.

In this article, we’ll look at how to alter and assign JavaScript object properties without side effects.

How to alter and assign JavaScript object properties without side effects?

To alter and assign JavaScript object properties without side effects, we should make a copy of the original object before assigning new properties to the copied object.

For instance, we write:

const a = {
  id: 0,
  value: 1
}
const b = {
  ...a,
  id: 1
}
console.log(a === b)
console.log(b)

to make a copy of a with the spread operator and then we the id property to 1.

We can see that the console log logs false so we know a and b aren’t referencing the same object.

And we see that b is {id: 1, value: 1}.

Conclusion

To alter and assign JavaScript object properties without side effects, we should make a copy of the original object before assigning new properties to the copied object.

Categories
JavaScript Answers

How to scroll down with JavaScript?

Sometimes, we want to scroll down with JavaScript.

In this article, we’ll look at how to scroll down with JavaScript.

How to scroll down with JavaScript?

To scroll down with JavaScript, we can use the window.scrollBy method.

For instance, we write:

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras vulputate neque vel dolor lobortis, a rutrum turpis sollicitudin. Praesent faucibus imperdiet euismod. Donec vitae nisl pretium, elementum nibh ac, aliquam massa. Proin eleifend mi sit amet urna consectetur, vel convallis nunc dictum. Nulla elementum, eros in congue dignissim, mi lectus aliquet augue, sed sodales odio nunc vitae nibh. Nunc id enim purus. Cras sollicitudin rhoncus enim, non tincidunt leo pellentesque nec. Etiam tristique urna eget arcu euismod, et vestibulum sem imperdiet. Morbi ultricies laoreet purus at viverra. Sed tincidunt diam quis lorem hendrerit, vitae dapibus augue congue. Aliquam erat volutpat. Curabitur lacinia libero vel metus consequat congue. Praesent nec magna quis est egestas facilisis sed id turpis. Sed finibus nulla eget purus viverra viverra. Mauris ornare quam vitae sapien tempor, eget consectetur orci maximus.
</div>

to add some content.

Then we write:

window.scrollBy(0,180);

to scroll right by 0 pixels and down by 180 pixels.

Conclusion

To scroll down with JavaScript, we can use the window.scrollBy method.

Categories
JavaScript Answers

How to change order of divs with jQuery?

Sometimes, we want to change order of divs with jQuery

In this article, we’ll look at how to change order of divs with jQuery.

How to change order of divs with jQuery?

To change order of divs with jQuery, we can use the insertAfter method.

For instance, we write:

<div class="test one">aaa</div>
<div class="test two">bbb</div>
<div class="test three">ccc</div>

to add 3 divs.

Then we take the first div and insert it after the 3rd with:

const tests = $('.test');
tests.first().insertAfter(tests.last());

We select all 3 divs with class test with $('.test').

Then we get the first of the elements with first.

And we call insertAfter with the element to insert the first element after.

We called it with tests.last() so we insert the first element after the last element.

Therefore, we see:

bbb
ccc
aaa

displayed.

Conclusion

To change order of divs with jQuery, we can use the insertAfter method.

Categories
JavaScript Answers

How to round money amount to the nearest 10 dollars in JavaScript?

Sometimes, we want to round money amount to the nearest 10 dollars in JavaScript.

In this article, we’ll look at how to round money amount to the nearest 10 dollars in JavaScript.

How to round money amount to the nearest 10 dollars in JavaScript?

To round money amount to the nearest 10 dollars in JavaScript, we can use the Math.round method.

For instance, we write:

const val = 123.45
const rounded = Math.round(val / 10) * 10;
console.log(rounded)

to round val to the nearest 10 dollars.

To do this, we first divide val by 10.

Then we round the quotient to the nearest integer with Math.round.

Finally, we divide the rounded number by 10 to return val rounded to the nearest 10 dollars.

Therefore, val is 120.

Conclusion

To round money amount to the nearest 10 dollars in JavaScript, we can use the Math.round method.

Categories
JavaScript Answers

How to convert a JavaScript object to array?

Sometimes, we want to convert a JavaScript object to array.

In this article, we’ll look at how to convert a JavaScript object to array.

How to convert a JavaScript object to array?

To convert a JavaScript object to array, we can use the Object.values method to return the object property values in an array.

For instance, we write:

const o = {
  0: "a",
  1: 'b'
};
const a = Object.values(o);
console.log(a)

To call Object.values with o.

Then we see that a is ['a', 'b'] from the console log.

Conclusion

To convert a JavaScript object to array, we can use the Object.values method to return the object property values in an array.