Answers for "javascript array methods"

4

javascript array functions

concat(arr1,[...]) // Joins two or more arrays, and returns a copy of the joined arrays
copyWithin(target,[start],[end]) // Copies array elements within the array, to and from specified positions
entries() // Returns a key/value pair Array Iteration Object
every(function(currentval,[index],[arr]),[thisVal]) // Checks if every element in an array pass a test
fill(val,[start],[end]) // Fill the elements in an array with a static value
filter(function(currentval,[index],[arr]),[thisVal]) //	Creates a new array with every element in an array that pass a test
find(function(currentval,[index],[arr]),[thisVal]) // Returns the value of the first element in an array that pass a test
findIndex(function(currentval,[index],[arr]),[thisVal]) // Returns the index of the first element in an array that pass a test
forEach(function(currentval,[index],[arr]),[thisVal]) // Calls a function for each array element
from(obj,[mapFunc],[thisVal]) // Creates an array from an object
includes(element,[start]) // Check if an array contains the specified element
indexOf(element,[start]) // Search the array for an element and returns its position
isArray(obj) // Checks whether an object is an array
join([seperator]) // Joins all elements of an array into a string
keys() // Returns a Array Iteration Object, containing the keys of the original array
lastIndexOf(element,[start]) // Search the array for an element, starting at the end, and returns its position
map(function(currentval,[index],[arr]),[thisVal]) // Creates a new array with the result of calling a function for each array element
pop() // Removes the last element of an array, and returns that element
push(item1,[...]) // Adds new elements to the end of an array, and returns the new length
reduce(function(total,currentval,[index],[arr]),[initVal]) // Reduce the values of an array to a single value (going left-to-right)
reduceRight(function(total,currentval,[index],[arr]),[initVal]) // Reduce the values of an array to a single value (going right-to-left)
reverse() // Reverses the order of the elements in an array
shift() // Removes the first element of an array, and returns that element
slice([start],[end]) // Selects a part of an array, and returns the new array
some(function(currentval,[index],[arr]),[thisVal]) // Checks if any of the elements in an array pass a test
sort([compareFunc]) // Sorts the elements of an array
splice(index,[quantity],[item1,...]) // Adds/Removes elements from an array
toString() // Converts an array to a string, and returns the result
unshift(item1,...) // Adds new elements to the beginning of an array, and returns the new length
valueOf() // Returns the primitive value of an array
Posted by: Guest on March-30-2021
14

javascript array methods

let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2
Posted by: Guest on June-25-2020
1

Array Methods

dogs.toString();                        // convert to string: results "Bulldog,Beagle,Labrador"
dogs.join(" * ");                       // join: "Bulldog * Beagle * Labrador"
dogs.pop();                             // remove last element
dogs.push("Chihuahua");                 // add new element to the end
dogs[dogs.length] = "Chihuahua";        // the same as push
dogs.shift();                           // remove first element
dogs.unshift("Chihuahua");              // add new element to the beginning
delete dogs[0];                         // change element to undefined (not recommended)
dogs.splice(2, 0, "Pug", "Boxer");      // add elements (where, how many to remove, element list)
var animals = dogs.concat(cats,birds);  // join two arrays (dogs followed by cats and birds)
dogs.slice(1,4);                        // elements from [1] to [4-1]
dogs.sort();                            // sort string alphabetically
dogs.reverse();                         // sort string in descending order
x.sort(function(a, b){return a - b});   // numeric sort
x.sort(function(a, b){return b - a});   // numeric descending sort
highest = x[0];                         // first item in sorted array is the lowest (or highest) value
x.sort(function(a, b){return 0.5 - Math.random()});     // random order sort
Posted by: Guest on July-24-2021
0

javascript array methods

let a = ['sam','john','ali','kahn'];
let b = [19,23,41,61];
let p = 'var val';

//  #### arrays all type to declera

 let a = [34,'osama',929,'ali','kahn']; //1st to declar arry
 let a = new Array();  //when you dont know how many is their

let mlutiarry = [
    ['osama','khan','ali'],     //array in array
    [12,45,34],
    ['audit','val','naztown']
];

//   #### arrays all type to declera

 let r = Array.isArray(b);  //check is arry or not
 let c = a.includes("ali"); //check wether value is in arry or not (its case sensetive)

 a.sort();  //sort all value
 a.reverse();  //reverse all value

 let st = a.toString();   //array into string
 let st = a.join("-");   // same but without ,
 let n = a.concat(b); //join two make one arry

 a.pop();  //delete end val in array
 a.shift(); //delete start val in arry

 a.push("new"); // add new val to end
 a.unshift("new"); //add new val to start

 a[2] = "ali new"; //add modify/update value frome array any where you want
 delete a[0]; //delete value frome array any where you want but still undefine

 let n = a.slice(1,3); //slice new arry for exiting array;
 a.splice(2,0,"osama"); //splice used to add new items to an array:

 let i = a.indexOf("ali"); //give first indexof vale
 let i  = b.lastIndexOf(19); //gae last indexof vale

 let res = b.some(function(age) {  //if some is match retrun true
    return age >= 18;
})
let res = b.every(function(age) { //if every is macth return true
    return age >= 18;
})

let res = b.find(function (age) { //return first value
    return age >= 29;
})

let res = b.findIndex(function (age) { //return first value indexs
    return age >= 20;
})

let ar = [123,12,34];
let r = ar.map(function (x) { // return new array 
    return x * 10;
})
document.write(res + "<br");





let s = "this is an strig methods";
let s2 = "yeh i use to it";
let s3 = "this is end of it";



 let r = s.length; //use to get length with space

 let r = s.toUpperCase(); //upper all words
 let r = s.toLowerCase(); //lower all words

 let r = s.includes("an");   //return true if he find (casesensetive)
 let r = s.startsWith("th");// return true if he find fist-word (casesen..)
 let r = s.endsWith("ds"); //retrun true if the find last-word (cseseen..)

 let r = s.search("strig");  //return index if he find  (cases...)

 let r = s.match(/is/g);  //make new array with all same words

 let r = s.indexOf("is");  //return index of first world only (casesen...)
 let r = s.lastIndexOf("is");  //return index of last world only (casee...)

 let r = s.replace("methods","function"); // replace the first value only

 let r = s.trim();// trim form left to right

 let r = s.charAt(5); //retrun char on string
 let r = s.charCodeAt(5); //return unicode of char
 let r = String.fromCharCode(65); // put unicode and return char

 let r = s.concat(s2,s3); //join two string together

 let r = s.split(" "); //split string and make new array

 let r = s.repeat(3); //repeat same string as mush as you want

 let r = s.slice(3,20); //return new string where ever you want
 let r = s.substr(start,how many);
 let r = s. substring(start,how many - 1);
document.write(r);


 
// this is an number methods######

let n = 9.4356775;


let r = Number(n); //convert string into number

let r = parseInt(n); //convert decimal into int
let r = parseFloat(n); //return with decimal num if he is

let r = Number.isFinite(n); // return true int
let r = Number.isInteger(n); return true only on int

let r = n.toFixed(2); // return int with decimal as many as you want
let r = n.toPrecision(4); // return int with decimal as many as you want +1 last decimal



Math fundtion#########

let r = Math.ceil(2.3); //up value 3 -2 
let r = Math.floor(2.3); // down value 2 -3

let r = Math.round(3.5); //less then 5 is 3 up is 4 round
let r = Math.trunc(3.34); //convert into integer

let r = Math.min(12,423,464,3); //return min value 3
let r = Math.max(12,423,464,3); //return max value 464

let r = Math.sqrt(25); return (5)5
let r = Math.cbrt(125); return 5(5)(5)

let r = Math.pow(2,4); 2*2*2*2=16

let r = Math.random(); between 0 to 1

let r = Math.abs(-19); return absolut value
let r = Math.PI; return 3.141592653589793
document.write(r);
Posted by: Guest on October-09-2021
3

array methods in javascript

<script>    
    // Defining function to get unique values from an array
    function getUnique(array){
        var uniqueArray = [];
        
        // Loop through array values
        for(var value of array){
            if(uniqueArray.indexOf(value) === -1){
                uniqueArray.push(value);
            }
        }
        return uniqueArray;
    }
    
    var names = ["John", "Peter", "Clark", "Harry", "John", "Alice"];
    var uniqueNames = getUnique(names);
    console.log(uniqueNames); // Prints: ["John", "Peter", "Clark", "Harry", "Alice"]
</script>
Posted by: Guest on March-09-2020

Code answers related to "javascript array methods"

Code answers related to "Javascript"

Browse Popular Code Answers by Language