how to shuffle an array javascript


/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = round(random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}

shuffle(array);

function randomArrayShuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var alphabet=["a","b","c","d","e"];
randomArrayShuffle(alphabet);
//alphabet is now shuffled randomly = ["d", "c", "b", "e", "a"]



function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

yourArray.sort(function() { return 0.5 - Math.random() });

www.codegrepper.com
www.stackoverflow.com

how to split an array in javascript


var str = "How are you doing today?";

var res = str.split(" ");

// Returns new array from exising one
arr.slice(start, end);

// Example array.
let randomArray = [3, 5, 1, 5, 7,];
// Create an empty array.
let arrayOfArrays = [];

function splitArray( array ) {
while (array.length > 0) {
let arrayElement = array.splice(0,1);
arrayOfArrays.push(arrayElement);
}
return arrayOfArrays;
}

// Call the function while passing in an array of your choice.
splitArray(randomArray)
// => [ [ 3 ], [ 5 ], [ 1 ], [ 5 ], [ 7 ] ]

var myString = 'no,u';
var MyArray = myString.split(',');//splits the text up in chunks

let data = 'Child 1 First Name: AlinChild 1 Gender: FemalenChild 1 Hair Color: BlondenChild 1 Hair Style: WavynChild 1 Skin Tone: TannChild 2 First Name: Morgan nChild 2 Gender: FemalenChild 2 Hair Color: BrownnChild 2 Hair Style: PonytailnChild 2 Skin Tone: LightnRelationship 1 to 2: BrothernRelationship 2 to 1: Brothern';
let target = {};

data.split('n').forEach((pair) => {
if(pair !== '') {
let splitpair = pair.split(': ');
let key = splitpair[0].charAt(0).toLowerCase() + splitpair[0].slice(1).split(' ').join('');
target[key] = splitpair[1];
}
});

console.dir(target);

www.codegrepper.com
www.stackoverflow.com

HOW TO SPLIT AN ARRAY JAVASCRIPT


array.splice(index, number, item1, ....., itemN)

var str = "How are you doing today?";

var res = str.split(" ");

// Example array.
let randomArray = [3, 5, 1, 5, 7,];
// Create an empty array.
let arrayOfArrays = [];

function splitArray( array ) {
while (array.length > 0) {
let arrayElement = array.splice(0,1);
arrayOfArrays.push(arrayElement);
}
return arrayOfArrays;
}

// Call the function while passing in an array of your choice.
splitArray(randomArray)
// => [ [ 3 ], [ 5 ], [ 1 ], [ 5 ], [ 7 ] ]

//split into array of strings.
var str = "Well, how, are , we , doing, today";
var res = str.split(",");

www.codegrepper.com
www.stackoverflow.com

how to set an attribute to ignore null value json c#


[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
// or
[JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)]

// or for all properties in a class
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]

www.codegrepper.com
www.stackoverflow.com

how to send an alert in javascript


//you can either do this:

function mousePressed() {
//you can have any message
alert("I'm the best");
}

//or you can do this:

function keyPressed() {
//you can do any key
if (keyCode === UP_ARROW) {
//and any message
alert(number = random(0, 500));
}
}

//or you can do an (if) statement

if(x > width) {
alert("yay you did it");
}

Alert in JS doesn't return anything -- it just Displays Message

www.codegrepper.com
www.stackoverflow.com

how to scroll to an element javascript react


class ReadyToScroll extends Component {

constructor(props) {
super(props)
this.myRef = React.createRef()
}

render() {
return <div ref={this.myRef}></div>
}

scrollToMyRef = () => window.scrollTo(0, this.myRef.current.offsetTop)
// run this method to execute scrolling.

}

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo(0, ref.current.offsetTop)
// General scroll to element function

const ScrollDemo = () => {

const myRef = useRef(null)
const executeScroll = () => scrollToRef(myRef)

return (
<>
<div ref={myRef}>I wanna be seen</div>
<button onClick={executeScroll}> Click to scroll </button>
</>
)
}

import { scroller } from "react-scroll";

// excluded React component syntax...

scrollToSection = () => {
scroller.scrollTo("your_css_class_here", {
duration: 800,
delay: 0,
smooth: "easeInOutQuart",
});
};

www.codegrepper.com
www.stackoverflow.com