DEV Community

Cover image for [week2] Days 1 - JS data structure
Black-Thor
Black-Thor

Posted on • Updated on

[week2] Days 1 - JS data structure

Back for 100 days of code

After 1 week without the ability to post because I got electrical trouble in my house, I follow my ways through JS learning course on freeCodeCamp.

Basic Data Structure

Data structures, at a high level, are techniques for storing and organizing data that make it easier to modify, navigate, and access. Data structures determine how data is collected, the functions we can use to access it, and the relationships between data.

The most basic of all data structures, an array, stores data in memory for later use. Each array has a fixed number of cells decided on its creation

array explaining image

How to manipulate

Use an Array to Store a Collection of Data

You can use let to make an array which store data,
example on a one dimensional array :

let simpleArray = ['one', 2, 'three', true, false, undefined, null];
Enter fullscreen mode Exit fullscreen mode

array can store complex object with multidimensional array

Access an Array's Contents Using Bracket Notation

You can access your data in your array.
In an array, each array item has an index. This index doubles as the position of that item in the array, and how you reference it
int's important to not forget that JavaScript is a zero-indexed, meaning that the first element of an array is actually at the zeroth position this is how to retrieve data :

let ourVariable = ourArray[0];
how to add a variable iin the array 
ourArray[1] = "not b anymore";
Enter fullscreen mode Exit fullscreen mode

Add Items to an Array with push() and unshift()

Array.push() to add an element to the end of an array
Array.unshift() to add the element to the beginning
you can pass a variable with these two methods, but not array

Remove Items from an Array with pop() and shift()

pop() removes an element from the end of an array,
shift() removes an element from the beginning.

You cannot parameter with this two method and u can change only array

Remove Items Using splice()

splice() allows us to remove any number of consecutive elements from anywhere in an array.

let array = ['today', 'was', 'not', 'so', 'great'];

array.splice(2, 2); //['today', 'was', 'great']
```



### Add Items Using splice()
`splice()` can take up to three parameters, and you can use the third parameter, comprised of one or more element(s), to add to an array.



```JavaScript
const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;

numbers.splice(startIndex, amountToDelete, 13, 14);
console.log(numbers);//[ 10, 11, 12, 13, 14, 15 ]
```



### Copy Array Items Using slice()



```JavaScript

let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];

let todaysWeather = weatherConditions.slice(1, 3);//['snow', 'sleet']
```


Enter fullscreen mode Exit fullscreen mode

Top comments (0)