Categories
JavaScript Answers

How to create a JavaScript associative array with negative integer keys?

Sometimes, we want to create a JavaScript associative array with negative integer keys.

In this article, we’ll look at how to create a JavaScript associative array with negative integer keys.

How to create a JavaScript associative array with negative integer keys?

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.

For instance, we write:

const o = {
  '-1': 'apple',
  '-2': 'orange'
}
console.log(o)

to define object o which has negative integers as keys.

Therefore, o is {-1: 'apple', -2: 'orange'} according to the console log.

Conclusion

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.

Categories
JavaScript Answers

How to return elements where ID begins with a certain string with JavaScript?

Sometimes, we want to return elements where ID begins with a certain string with JavaScript.

In this article, we’ll look at how to return elements where ID begins with a certain string with JavaScript.

How to return elements where ID begins with a certain string with JavaScript?

To return elements where ID begins with a certain string with JavaScript, we can select elements with querySelectorAll.

Then we can convert the returned node list into an array and use filter to return an array of elements with the given ID pattern.

For instance, we write:

<div id='foo1'>
  1
</div>
<div id='foo2'>
  2
</div>
<div id='bar'>
  3
</div>

to add divs with IDs.

Then we write:

const divs = document.querySelectorAll('div')
const fooDivs = [...divs]
  .filter(d => d.id.includes('foo'))
console.log(fooDivs)

We select all the divs with querySelectorAll.

Then we spread the divs into an array.

And then we call filter to return an array with the ones with ids that includes 'foo'.

Therefore, fooDivs should have the first 2 divs.

Conclusion

To return elements where ID begins with a certain string with JavaScript, we can select elements with querySelectorAll.

Then we can convert the returned node list into an array and use filter to return an array of elements with the given ID pattern.

Categories
JavaScript Answers

How to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify?

Sometimes, we want to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify.

In this article, we’ll look at how to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify.

How to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify?

To reduce numbers’ significance when stringifying values with JavaScript JSON.stringify, we can call JSON.stringify with a function that rounds numbers to the number of decimal places we want.

For instance, we write:

const a = [0.123456789123456789]
const strA = JSON.stringify(a, (key, val) => {
  return typeof val === 'number' ? Number(val.toFixed(3)) : val;
})
console.log(strA)

to call JSON.stringify with a and a callback that checks if the val value being stringified if a number.

If it is, we call val.toFixed with 3 to round val to 3 decimal places.

Otherwise, we return val as is.

Therefore, strA is '[0.123]'.

Conclusion

To reduce numbers’ significance when stringifying values with JavaScript JSON.stringify, we can call JSON.stringify with a function that rounds numbers to the number of decimal places we want.

Categories
JavaScript Answers

How to print objects in Node.js?

Sometimes, we want to print objects in Node.js.

In this article, we’ll look at how to print objects in Node.js.

How to print objects in Node.js?

To print objects in Node.js, we can use the util.inspect method.

For instance, we write:

const util = require('util')
const obj = {
  a: 0,
  b: 0
}
console.log(util.inspect(obj, {
  depth: null
}));

We call util.inspect with obj and { depth: null } to print the whole obj object.

Therefore, we see { a: 0, b: 0 } logged.

Conclusion

To print objects in Node.js, we can use the util.inspect method.

Categories
JavaScript Answers

How to convert a string array to an array in JavaScript?

Sometimes, we want to convert a string array to an array in JavaScript.

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

How to convert a string array to an array in JavaScript?

To convert a string array to an array in JavaScript, we can use the JSON.parse method.

For instance, we write:

const services = '["service1", "service2", "service3"]'
const s = JSON.parse(services)
console.log(s)

We call JSON.parse with services to parse it into a JavaScript array.

Therefore, s is ['service1', 'service2', 'service3'].

Conclusion

To convert a string array to an array in JavaScript, we can use the JSON.parse method.