How to get one item from an array with the name name and value with JavaScript?

Sometimes, we want to get one item from an array with the name name and value with JavaScript.

In this article, we’ll look at how to get one item from an array with the name name and value with JavaScript.

How to get one item from an array with the name name and value with JavaScript?

To get one item from an array with the name name and value with JavaScript, we use the array find method.

For instance, we write

const arr = [];
arr.push({ name: "k1", value: "abc" });
arr.push({ name: "k2", value: "hi" });
arr.push({ name: "k3", value: "oa" });

const found = arr.find((item) => {
  return item.name === "k1";
});

console.log("found", found);

to call arr.find with a callback that checks for the first object in arr with name property 'k1'.

Then found has the item as its value.

Conclusion

To get one item from an array with the name name and value with JavaScript, we use the array find method.