Categories
JavaScript Answers

How to Declare an Empty Two-Dimensional Array in JavaScript?

Sometimes, we want to declare an empty two-dimensional array in JavaScript.

In this article, we’ll look at how to declare an empty two-dimensional array in JavaScript.

Declare an Empty Two-Dimensional Array in JavaScript

To declare an empty two-dimensional array in JavaScript, we can create an empty array with the Array constructor.

Then we call map on that array with a callback that returns an array with the Array constructor to create the nested arrays.

For instance, we can write:

const nested = [...Array(3)].map(x => Array(5).fill(0))
console.log(nested)

We call Array with 3 to create an array with length 3.

Then we spread that into a new array to create a copy of it.

Next, we call map on the copied array with a callback that returns an array with length with all values in it set to 0 with Array(5).fill(0) .

As a result, nested is a two-dimensional array with length 3, with each nested array with length 5 and all values inside it set to 0.

If we want the nested arrays to be empty, we can omit the fill call.

Conclusion

To declare an empty two-dimensional array in JavaScript, we can create an empty array with the Array constructor.

Then we call map on that array with a callback that returns an array with the Array constructor to create the nested arrays.

Categories
JavaScript Answers

How to Check if the Array of Objects Have Duplicate Property Values with JavaScript?

Sometimes, we want to check if the array of objects have duplicate property values with JavaScript.

In this article, we’ll look at how to check if the array of objects have duplicate property values with JavaScript.

Check if the Array of Objects Have Duplicate Property Values with JavaScript

To check if the array of objects have duplicate property values with JavaScript, we can use the JavaScript array’s map and some method to map the array to the property values we want to check.

Then we can use the indexOf method in the some callback to check if an element in the mapped array is the first instance or not.

For instance, we can write:

const values = [{
    name: 'someName1'
  },
  {
    name: 'someName2'
  },
  {
    name: 'someName4'
  },
  {
    name: 'someName2'
  }
];

const valueArr = values.map((item) => {
  return item.name
});
const isDuplicate = valueArr.some((item, idx) => {
  return valueArr.indexOf(item) !== idx
});
console.log(isDuplicate);

We have the values array that we want to check if there’re duplicate values of the name property.

Then we call map to return an array with the name property values and assign it to valueArr .

Next, we call valueArr.some with a callback that checks if idx is different from the index returned by indexOf .

Since indexOf returns the index of the first instance of a value, that means if idx is different from what’s returned with indexOf , we know it’s a duplicate value.

Therefore, since we have a duplicate value of name in 2 entries, isDuplicate is true and that’s logged in the console log.

Conclusion

To check if the array of objects have duplicate property values with JavaScript, we can use the JavaScript array’s map and some method to map the array to the property values we want to check.

Then we can use the indexOf method in the some callback to check if an element in the mapped array is the first instance or not.

Categories
JavaScript Answers

How to Play Looping Audio with the HTML5 Audio Element and JavaScript?

Sometimes, we want to play looping audio with the HTML5 audio element and JavaScript.

In this article, we’ll look at how to play looping audio with the HTML5 audio element and JavaScript.

Play Looping Audio with the HTML5 Audio Element and JavaScript

To play looping audio with the HTML5 audio element and JavaScript, we can listen for the ended event and call play on the audio element in event handler for the ended event.

For instance, we can write:

const myAudio = new Audio('https://file-examples-com.github.io/uploads/2017/11/file_example_OOG_1MG.ogg');
myAudio.addEventListener('ended', () => {
  myAudio.currentTime = 0;
  myAudio.play();
});
myAudio.play();

We create the HTML audio element with the Audio constructor and the URL of the audio clip.

Then we listen to the ended event by calling addEventListener on it.

In the event handler callback, we set the currentTime back to 0 and call play to restart the same clip.

Then we call play after that to start playing the audio clip for the first time.

Conclusion

To play looping audio with the HTML5 audio element and JavaScript, we can listen for the ended event and call play on the audio element in event handler for the ended event.

Categories
JavaScript Answers

How to Get a JSON Object’s Key and Value in JavaScript?

Sometimes, we want to get a JSON object’s key and value in JavaScript.

In this article, we’ll look at how to get a JSON object’s key and value in JavaScript.

Get a JSON Object’s Key and Value in JavaScript

To get a JSON object’s key and value in JavaScript, we can use the JSON.parse method to parse the JSON object string into a JavaScript object.

Then we can get a property’s value as we do with any other JavaScript object.

For instance, we can write:

const data = `{
  "name": "",
  "skills": "",
  "jobtitel": "Entwickler",
  "res_linkedin": "webSearch"
}`

const parsedData = JSON.parse(data);
console.log(parsedData.name);
console.log(parsedData.skills);
console.log(parsedData.jobtitel);
console.log(parsedData.res_linkedin);

We have the data JSON string that we parse into an object with JSON.parse and assigned it to parsedData .

Then we can get the properties from parsedData with the dot notation as we do with any other JavaScript object.

Conclusion

To get a JSON object’s key and value in JavaScript, we can use the JSON.parse method to parse the JSON object string into a JavaScript object.

Then we can get a property’s value as we do with any other JavaScript object.

Categories
JavaScript Answers

How to Select All Child Elements Except the First with JavaScript?

Sometimes, we want to select all child elements except the first with JavaScript.

In this article, we’ll look at how to select all child elements except the first with JavaScript.

Select All Child Elements Except the First with JavaScript

To select all child elements except the first with JavaScript, we can use jQuery with the not pseudo-selector to select all elements except the first element.

For instance, if we have the following HTML:

<ul>  
  <li>First item</li>  
  <li>Second item</li>  
  <li>Third item</li>  
</ul>

and we want to select the last 2 li elements, then we can write:

$("li:not(:first-child)").addClass("something");

to get all the li’s that isn’t the first child of the ul element with li:not(:first-child) .

Then we can call addClass on all the selected elements to add the 'something' class to it.

We can also use the gt pseudo-selector to do the same thing.

For instance, we can write:

$("li:gt(0)").addClass("something");

gt(0) means we select every li but the first child.

We can also use the not method to do the same selection with:

$("li").not(':first').addClass("something");

not(‘:first’) also excludes the first child.

Conclusion

To select all child elements except the first with JavaScript, we can use jQuery with the not pseudo-selector to select all elements except the first element.