How to get the index of an array? Now I get undefined as the result of executing the "blockCreate" function, I think this is because I do not specify the index of the array, although I should get true, how to automatically indicate the index for the array?
import React, { useState } from "react";
import "./styles.css";
export default function App() {
const [array1, setArray1] = useState([{ number: 1 }]);
const [array2, setArray2] = useState([{ number: 2 }]);
const blockCreate = () => {
return array1.find(items => {
const bool = array2.find(i => i.number === items.number);
console.log(bool);
});
};
return (
<div className="App">
<div>{blockCreate()}</div>
</div>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
blockCreate()is supposed to do, in particular?array1.findfunction should returntrueorfalsefindreturnsundefinedwhen there is nothing found.The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.