Hi dev,
In this article we will learn example about Array Excludes Value To New Array using javascript. Let's start …
Syntax & Example:
array-excludes-value-to-new-array.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>array-excludes-value-to-new-array</title>
<link rel="stylesheet" type="text/css" href="../style.css" />
</head>
<body>
<h1>19-array-excludes-value-to-new-array!</h1>
<h3>Create a method named 'excludes' which cut/excludes values from existing array and push to new array</h3>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Syntax & Example:
script.js
console.log('19-array-excludes-value-to-new-array');
function excludes(arrayToExclude, elementsToExcluded) {
console.log('arrayToExclude: ', arrayToExclude);
console.log('elementsToExcluded: ', elementsToExcluded);
const outputArray = [];
for(let curElement of arrayToExclude) {
if(!elementsToExcluded.includes(curElement)) {
outputArray.push(curElement)
}
}
return outputArray;
}
const versionArray = [1, 2, 5, 7, 2];
const newVesionArray = (excludes(versionArray, [2]));
console.log('newVesionArray: ', newVesionArray);
console.log('---------');
const ageArray = [21, 25, 22, 25, 30, 25, 30];
const newAgeArray = (excludes(ageArray, [25,30]));
console.log('newAgeArray: ', newAgeArray);
Hope it can help you…
Categories : JavaScript
Tags : HTML