Categories
JavaScript Answers

How to merge two JSON or JavaScript arrays in to one array?

Sometimes, we want to merge two JSON or JavaScript arrays in to one array.

In this article, we’ll look at how to merge two JSON or JavaScript arrays in to one array.

How to merge two JSON or JavaScript arrays in to one array?

To merge two JSON or JavaScript arrays in to one array, we use the concat method.

For instance, we write

const finalArr = json1.concat(json2);

to call json1.concat with the json2 array to return a new array with the entries of the json1 and json2 entries.

Conclusion

To merge two JSON or JavaScript arrays in to one array, we use the concat method.

Categories
JavaScript Answers

How to loop through a JavaScript object array?

Sometimes, we want to loop through a JavaScript object array.

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

How to loop through a JavaScript object array?

To loop through a JavaScript object array, we use the for-of loop.

For instance, we write

const messages = [
  {
    msgFrom: "13223821242",
    msgBody: "Hi there",
  },
  {
    msgFrom: "Bill",
    msgBody: "Hello!",
  },
];

for (const message of messages) {
  console.log(message);
}

to loop through the messages array with a for-of loop.

We log the value of the messages entry message in the loop body.

Conclusion

To loop through a JavaScript object array, we use the for-of loop.

Categories
JavaScript Answers

How to get the latest and oldest record in Mongoose.js and JavaScript?

Sometimes, we want to get the latest and oldest record in Mongoose.js and JavaScript.

In this article, we’ll look at how to get the latest and oldest record in Mongoose.js and JavaScript.

How to get the latest and oldest record in Mongoose.js and JavaScript?

To get the latest and oldest record in Mongoose.js and JavaScript, we use the find and sort methods.

For instance, we write

const latest = await Model.find().sort({ $natural: -1 }).limit(1);
const oldest = await Model.find().sort({ $natural: 1 }).limit(1);

to get the latest item by calling sort with { $natural: -1 } to sort items by descending chronological order.

And we call limit with 1 to get the first returned.

And we get the oldest item by calling sort with { $natural: 1 } to sort items by ascending chronological order.

And we call limit with 1 to get the first returned.

Conclusion

To get the latest and oldest record in Mongoose.js and JavaScript, we use the find and sort methods.

Categories
React Native Answers

How to close React Native modal by clicking on overlay with JavaScript?

Sometimes, we want to close React Native modal by clicking on overlay with JavaScript.

In this article, we’ll look at how to close React Native modal by clicking on overlay with JavaScript.

How to close React Native modal by clicking on overlay with JavaScript?

To close React Native modal by clicking on overlay with JavaScript, we set the visible prop.

For instrance, we write

import React, { useState } from "react";
import {
  Button,
  StyleSheet,
  View,
  TouchableOpacity,
  Modal,
} from "react-native";

const App = () => {
  const [show, setShow] = useState(false);
  return (
    <View>
      <TouchableOpacity style={{ marginTop: 200 }}>
        <Button title="show" onPress={() => setShow(!show)} />
      </TouchableOpacity>
      <Modal transparent={true} visible={show} animationType="slide">
        <TouchableOpacity
          activeOpacity={1}
          style={{ backgroundColor: "#000000aa", flex: 1 }}
          onPress={() => setShow(!show)}
        />

        <View style={{ backgroundColor: "#FFFFFF", flex: 1 }}>
          <View>
            <Button title="close" onPress={() => setShow(!show)} />
          </View>
        </View>
      </Modal>
    </View>
  );
};
export default App;

to set the visible prop to the show state.

If show is true, then the Modal is shown.

Otherwise, it’s hidden.

We call setShow to show the modal in the TouchableOpacity components.

Conclusion

To close React Native modal by clicking on overlay with JavaScript, we set the visible prop.

Categories
JavaScript Answers

How to fix Charts.js graph not scaling to canvas size with JavaScript?

Sometimes, we want to fix Charts.js graph not scaling to canvas size with JavaScript.

In this article, we’ll look at how to fix Charts.js graph not scaling to canvas size with JavaScript.

How to fix Charts.js graph not scaling to canvas size with JavaScript?

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.

For instance, we write

const ctx = document.getElementById("myChart").getContext("2d");
const myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    datasets: [
      {
        label: "My First dataset",
        data: [1, 2, 3, 2, 1, 2, 3, 4, 5, 4],
      },
    ],
  },
  options: {
    responsive: false,
  },
});

to set options.responsive to false to make the graph scale to the canvas size.

We get the canvas with getElementById and get its context with getContext.

Conclusion

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.