1. JavaScript Array splice() Method
Definition and Usage
The splice() method adds/removes items to/from an array, and returns the removed item(s).
Note: This method changes the original array.
Syntax
array.splice(index, howmany, item1, ....., itemX)
Parameter Values
Parameter | Description |
---|---|
index | Required. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array |
howmany | Optional. The number of items to be removed. If set to 0, no items will be removed |
item1, ..., itemX | Optional. The new item(s) to be added to the array |
Return Value: | A new Array, containing the removed items (if any) |
---|
Example
At position 2, add the new items, and remove 1 item:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
=> result = Banana,Orange,Lemon,Kiwi,Mango
Example
At position 2, remove 2 items:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);
Example
At position 2, remove 0 items and add 2 items:
fruits.splice(2, 0, "abc" , "Kiwi");
=> result = [Banana,Orange,abc,Kiwi,Apple,Mango]
2. valueOf() Method
<script>
function myFunction() {
var str = "Hello World!";
var res = str.valueOf();
document.getElementById("demo").innerHTML = res;
}
</script>
function myFunction() {
var str = "Hello World!";
var res = str.valueOf();
document.getElementById("demo").innerHTML = res;
}
</script>
3. Set and Map
Set:
mySet[Symbol.iterator]
Return value
TheSet
iterator function, which is the values()
function by default.Example:
vm.fileSetUpload = new Set();
var rmID = $('#btnRemoveSetupFileSrv').val();
vm.fileSetUpload.add(rmID.valueOf());
const iterator1 = vm.fileSetUpload[Symbol.iterator]();
vm.arrRmAttachFile = [];
for (var v of vm.fileSetUpload) {
vm.arrRmAttachFile.push(v);
}
Map:
vm.filesMapUpload = new Map();
for (var i = 0; i < e.length; i++) {
vm.filesMapUpload.set(e[i].name, e[i]);
}
var fNames = '';
vm.filesMapUpload.forEach(function(value, key, map) {
fNames = fNames + key + ":";
});
vm.filesMapUpload = new Map();
for (var i = 0; i < e.length; i++) {
vm.filesMapUpload.set(e[i].name, e[i]);
}
var fNames = '';
vm.filesMapUpload.forEach(function(value, key, map) {
fNames = fNames + key + ":";
});
var rmID = $('#btnRemoveSetupFileCli').val();
vm.filesMapUpload.delete(rmID);
vm.filesMapUpload.delete(rmID);
vm.filesArrayUpload = [];
vm.filesMapUpload.forEach(function(value, key, map) {
vm.filesArrayUpload.push(value);
});
vm.filesMapUpload.forEach(function(value, key, map) {
vm.filesArrayUpload.push(value);
});
4.Reduce method
The
link:
https://www.freecodecamp.org/news/reduce-f47a7da511a9/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
REF:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
reduce()
method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.link:
https://www.freecodecamp.org/news/reduce-f47a7da511a9/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
5.Array.prototype.map()
Themap()
method creates a new array populated with the results of calling a provided function on every element in the calling array.REF:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
0 comments:
Post a Comment