Categories
JavaScript Answers

How to Find Indexes of All Occurrences of an Element in a JavaScript Array?

We can find indexes of all occurrences of an element in a JavaScript array by using the JavaScript array’s reduce method.

For instance, we can write:

const arr = ["Nano", "Volvo", "BMW", "Nano", "VW", "Nano"].reduce((a, e, i) => {
  if (e === 'Nano')
    a.push(i);
  return a;
}, []);
console.log(arr)

to call reduce on the array where we want to get all indexes of the string 'Nano' from.

To do that, we call it with a callback that takes the a array that we’re going to push the indexes into.

e has the entry of the array we’re checking to see if it’s 'Nano' .

And i is the index of the array entry e .

If e is 'Nano' , we call push with i to push i into the a array.

And then we return a .

The 2nd argument is the initial value of a which is set to an empty array.

And so arr is [0, 3, 5] .

Categories
JavaScript Answers

How to Find Indexes of All Occurrences of an Element in a JavaScript Array?

We can find indexes of all occurrences of an element in a JavaScript array by using the JavaScript array’s reduce method.

For instance, we can write:

const arr = ["Nano", "Volvo", "BMW", "Nano", "VW", "Nano"].reduce((a, e, i) => {
  if (e === 'Nano')
    a.push(i);
  return a;
}, []);
console.log(arr)

to call reduce on the array where we want to get all indexes of the string 'Nano' from.

To do that, we call it with a callback that takes the a array that we’re going to push the indexes into.

e has the entry of the array we’re checking to see if it’s 'Nano' .

And i is the index of the array entry e .

If e is 'Nano' , we call push with i to push i into the a array.

And then we return a .

The 2nd argument is the initial value of a which is set to an empty array.

And so arr is [0, 3, 5] .

Categories
JavaScript Answers

How Remove Property for All Objects in a JavaScript Array?

We can use destructuring to remove a property from all objects in an array.

For instance, we can write:

const array = [{
  "bad": "something",
  "good": "something"
}, {
  "bad": "something",
  "good": "something"
}];

const newArray = array.map(({
  bad,
  ...keepAttrs
}) => keepAttrs)
console.log(newArray)

We want to remove the bad property from each object in array .

To do that, we call array.map with a callback that destructures the object by separating the bad property from the rest of the properties, which are kept in the keepAttrs object.

We return that so we get that as the value.

And so newArray is:

[
  {
    "good": "something"
  },
  {
    "good": "something"
  }
]
Categories
JavaScript Answers

How to Remove All the Child DOM Elements in a Div with JavaScript?

We can remove all child nodes from an element with a few node properties.

For instance, if we have the following HTML:

<div>  
  <p>  
    foo  
  </p>  
  <p>  
    bar  
  </p>  
  <p>  
    baz  
  </p>  
</div>

Then we can remove all the p elements by writing:

const node = document.querySelector('div')  
while (node.hasChildNodes()) {  
  node.removeChild(node.lastChild);  
}

We get the div with document.querySelector .

Then we use a while loop to check if there’re any child nodes left with node.hasChildNodes .

And if there are any left, we call node.removeChild with node.lastChild to remove the last child node.

Categories
JavaScript Answers

How to Convert JavaScript FormData to Object to JSON?

We can convert JavaScript form data objects to JSON strings easily with the Object.fromEntries and the JSON.strinnfiyt methods.

Object.fromEntries lets us convert an array of key-value pair arrays to an object.

And JSON.stringify lets us convert an object to a JSON string with the JSON object.

For instance, we can create a form:

<form>  
  Email <input name='email'>  
  Password <input name='password' type='password'>  
  <input type='submit'>  
</form>

Then we can listen to the submit event on the form with JavaScript:

const form = document.querySelector('form')  
form.addEventListener('submit', (e) => {  
  e.preventDefault()  
  const formData = new FormData(e.target)  
  const json = JSON.stringify(Object.fromEntries(formData));  
  console.log(json)  
})

We call addEventListener on the form to listen to the submit event.

In the submit event callback, we call e.preventDefault to prevent the default server-side submit behavior.

Then we create a form data object from the form with the FormData constructor.

e.target has the form element.

Next, we call Object.fromEntries to convert the formData into an object with the keys being the value of the name attribute of each input, and the value being the inputted value of each field.

And finally, we call JSON.stringify to convert the object into a JSON string with the object.

Therefore, json is something like:

{"email":"abc","password":"abc"}

if we typed in 'abc' into each field.