JSFiddle - React, Tailwind, and code Playground

by josekerekes

HTML

<div id="target"></div>
<div id="target2"></div>
<div id="target3"></div>
<div id="target4"></div>
<div id="target5"></div>

JavaScript

var arr = [10,20,30,40,50];

//delete arr[1] does not recalculate index of array elements
arr.splice(1,1)  //params are index and number of elements to delete, third param is what to add in that location/
arr.splice(4,0,5)
$('#target').prepend(arr);

var sum = [1, 2, 3].reduce(
  function(total, num){ return total + num }
  , 0);


$('#target2').prepend(sum)

$('#target3').prepend(arr.valueOf());

//other methods
// join, pop(removes last and returns it in a var)
// push (pushes a new element in array, return the length
// shift(deletes first)
// unshift
// delete changes the element in undefined
// sort(alpabetically as string)
// for numbers you provide compare function points.sort(function(a, b){return a-b});
// reverse
//concat
// slice - cuts out from array to a new array

//find the highest value
$('#target4').prepend(arr.sort(function(a,b) { return b-a })[0])
 


var duplicates=[1,2,1,2];

//we put in an object every value of array as value:value (it does not let duplicates) then put values to a new array

Array.prototype.unique = function() {
    var obj = {}, i, size = this.length, newArr = [];
    for(i=0; i<size;i+=1) obj[this[i]] = this[i];
    for(i in obj) newArr.push(obj[i]);
    return newArr;
};

//find duplicates
$('#target5').prepend(duplicates.unique())