Categories
JavaScript Answers

How to Get a Substring that Comes After a Separator with JavaScript?

Sometimes, we want to get a substring that comes after a separator with JavaScript.

In this article, we’ll look at how to get a substring that comes after a separator with JavaScript.

Get a Substring that Comes After a Separator with JavaScript

To get a substring that comes after a separator with JavaScript, we can use various string methods.

We can use the JavaScript string’s lastIndexOf method to get the last instance of a separator.

Then we can use the JavaScript string’s substr method to get the substring of a string.

For instance, we can write:

const str = "xxx_456";
const strSub = str.substr(str.lastIndexOf("_") + 1);
console.log(strSub)

We have the str string that uses the _ as a separator.

Then we call str.lastIndexOf with '_' to get the index of the last instance of '_' .

Then we add 1 to it to get the substring after the '_' .

And then we call substr to get the substring from str.lastIndexOf(“_”) + 1 to the end of the string.

Therefore, strSub is '456' .

Conclusion

To get a substring that comes after a separator with JavaScript, we can use various string methods.

We can use the JavaScript string’s lastIndexOf method to get the last instance of a separator.

Then we can use the JavaScript string’s substr method to get the substring of a string.

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.