Categories
JavaScript Answers

How to sum values from an array of key-value pairs in JavaScript?

Sometimes, we want to sum values from an array of key-value pairs in JavaScript.

In this article, we’ll look at how to sum values from an array of key-value pairs in JavaScript.

How to sum values from an array of key-value pairs in JavaScript?

To sum values from an array of key-value pairs in JavaScript, we use the array reduce method.

For instance, we write

const myData = [
  ["2022-01-22", 0],
  ["2022-01-29", 0],
  ["2022-02-05", 0],
  ["2022-02-12", 0],
  ["2022-02-19", 0],
  ["2022-02-26", 0],
  ["2022-03-05", 0],
  ["2022-03-12", 0],
  ["2022-03-19", 0],
  ["2022-03-26", 0],
  ["2022-04-02", 21],
  ["2022-04-09", 2],
];
const sum = myData
  .map(([, val]) => val)
  .reduce((sum, current) => sum + current, 0);

console.log(sum);

to call myData.map to map the myData array into an array of values from the 2nd entry of each array.

Then we call reduce to return the partial sum of the array by adding sum to current and returning the sum.

0 is the initial value of sum.

current is the value being looped through

Conclusion

To sum values from an array of key-value pairs in JavaScript, we use the array reduce method.

Categories
JavaScript Answers

How to serialize an input File object to JSON with JavaScript?

Sometimes, we want to serialize an input File object to JSON with JavaScript.

In this article, we’ll look at how to serialize an input File object to JSON with JavaScript.

How to serialize an input File object to JSON with JavaScript?

To serialize an input File object to JSON with JavaScript, we extract the properties from a file object and put them in a new object.

For instance, we write

const { lastModified, lastModifiedDate, name, size, type } = fileObject;
const newObject = {
  lastModified,
  lastModifiedDate,
  name,
  size,
  type,
};

const fileJson = JSON.stringify(newObject);

to get the propeties from the fileObject file object with destructuring.

Then we put the properties in a new object.

Next, we call JSON.stringify with newObject to convert it into a JSON string.

Conclusion

To serialize an input File object to JSON with JavaScript, we extract the properties from a file object and put them in a new object.

Categories
JavaScript Answers

How to create a fixed length array in JavaScript?

Sometimes, we want to create a fixed length array in JavaScript.

In this article, we’ll look at how to create a fixed length array in JavaScript.

How to create a fixed length array in JavaScript?

To create a fixed length array in JavaScript, we use the Object.seal method.

For instance, we write

const a = new Array(100);
a.fill(1);
Object.seal(a);

to create array a with length 100.

Then we call fill to fill all the slots with 1’s.

Finally, we call Object.seal with a to stop its length from being changed.

Conclusion

To create a fixed length array in JavaScript, we use the Object.seal method.

Categories
JavaScript Answers

How to stream response in Express and JavaScript?

Sometimes, we want to stream response in Express and JavaScript.

In this article, we’ll look at how to stream response in Express and JavaScript.

How to stream response in Express and JavaScript?

To stream response in Express and JavaScript, we use res.write.

For instance, we write

app.get("/report", (req, res) => {
  res.write("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n");

  for (let i = 0; i < 10; i++) {
    res.write("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n");
  }

  res.end();
});

to call res.write to put each line in the response.

Then we call res.end to stop sending the lines.

Conclusion

To stream response in Express and JavaScript, we use res.write.

Categories
JavaScript Answers

How to define a regular expression to validate US phone numbers with JavaScript?

Sometimes, we want to define a regular expression to validate US phone numbers with JavaScript.

In this article, we’ll look at how to define a regular expression to validate US phone numbers with JavaScript.

How to define a regular expression to validate US phone numbers with JavaScript?

To define a regular expression to validate US phone numbers with JavaScript, we write a regex to match the groups of numbers.

For instance, we write

const re = /^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/;

to use [0-9]{3} to match the groups of 3 digits.

We use [0-9]{4} to match the group of 4 digits.

^ matches start of the string.

$ matches end of the string.

Conclusion

To define a regular expression to validate US phone numbers with JavaScript, we write a regex to match the groups of numbers.