DEV Community

Black-Thor
Black-Thor

Posted on • Updated on

[week2] Days 1 - JS data structure part 2

After some time with a lot of problems , i am still in the way to improve my self . I dont reset because i only make 1 week in the 100 day of code .
So back to business , this is what i learned today

Copy an Array with the Spread Operator

You can copy easily the content of another array with this method:

let thatArray = [...thisArray];
Enter fullscreen mode Exit fullscreen mode

Combine Arrays with the Spread Operator

You can put in a specific place your new data by using your spread operator in the middle of an array .

let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];

let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
Enter fullscreen mode Exit fullscreen mode

Check For The Presence of an Element With indexOf()

indexOf() takes an element as a parameter, and when called, it returns the position, or index, of that element, or -1 if the element does not exist on the array.

let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];

fruits.indexOf('dates');//-1
fruits.indexOf('pears');//2
Enter fullscreen mode Exit fullscreen mode

Iterate Through All an Array's Items Using For Loops

You can use iteration to move in a multidimensional array and use indexOf() like this

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  for ( let i=0 ; i<arr.length; i++){
    if (arr[i].indexOf(elem) == -1) {
      newArr.push(arr[i]); 
}
  }
  // Only change code above this line
  return newArr;//[[10, 8, 3], [14, 6, 23]]
}

console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18))
Enter fullscreen mode Exit fullscreen mode

Create complex multidimensional arrays

One of the most powerful features when thinking of arrays as data structures, is that arrays can contain, or even be completely made up of other arrays. We have seen arrays that contain arrays in previous challenges, but fairly simple ones. However, arrays can contain an infinite depth of arrays that can contain other arrays, each with their own arbitrary levels of depth, and so on. In this way, an array can very quickly become a very complex data structure, known as a multi-dimensional, or nested array. Consider the following example:

let nestedArray = [
  ['deep'],
  [
    ['deeper'], ['deeper'] 
  ],
  [
    [
      ['deepest'], ['deepest']
    ],
    [
      [
        ['deepest-est?']
      ]
    ]
  ]
];
Enter fullscreen mode Exit fullscreen mode

The deep array is nested 2 levels deep. The deeper arrays are 3 levels deep. The deepest arrays are 4 levels, and the deepest-est? is 5.

Add Key-Value Pairs to JavaScript Objects

At their most basic, objects are just collections of key-value pairs. In other words, they are pieces of data (values) mapped to unique identifiers called properties (keys). Take a look at an example:

const tekkenCharacter = {
  player: 'Hwoarang',
  fightingStyle: 'Tae Kwon Doe',
  human: true
};
Enter fullscreen mode Exit fullscreen mode

add property

tekkenCharacter.origin = 'South Korea';
Enter fullscreen mode Exit fullscreen mode

You can add this property with bracket notation by doing:

tekkenCharacter['hair color'] = 'dyed orange';
Enter fullscreen mode Exit fullscreen mode

Without quotes, it will be evaluated as a variable and the name of the property will be whatever value the variable is. Here's an example with a variable:

const eyes = 'eye color';

tekkenCharacter[eyes] = 'brown';
Enter fullscreen mode Exit fullscreen mode

at the end this is the final result

final result 
{
  player: 'Hwoarang',
  fightingStyle: 'Tae Kwon Doe',
  human: true,
  origin: 'South Korea',
  'hair color': 'dyed orange',
  'eye color': 'brown'
};
Enter fullscreen mode Exit fullscreen mode

Modify an Object Nested Within an Object

Inside a nested object, you can modify the data by specifying what you want to change

let nestedObject = {
  id: 28802695164,
  date: 'December 31, 2016',
  data: {
    totalUsers: 99,
    online: 80,
    onlineStatus: {
      active: 67,
      away: 13,
      busy: 8
    }
  }
};

nestedObject.data.onlineStatus.busy = 10; //modification
Enter fullscreen mode Exit fullscreen mode

Access Property Names with Bracket Notation

You can get the data inside an object like this

let inventory = foods[selectedFood];
Enter fullscreen mode Exit fullscreen mode

Use the delete Keyword to Remove Object Properties

You can delete the data inside an object like this

delete foods.apples;
Enter fullscreen mode Exit fullscreen mode

Check if an Object has a Property

One uses the hasOwnProperty() method and the other uses the in keyword. If we have an object users with a property of Alan, we could check for its presence in either of the following ways:

users.hasOwnProperty('Alan');
'Alan' in users;
Enter fullscreen mode Exit fullscreen mode

Iterate Through the Keys of an Object with a for...in Statement

Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a for...in statement. For our users object, this could look like:

for (let user in users) {
  console.log(user);
}
Enter fullscreen mode Exit fullscreen mode

New training site

I discover a website where I can test what I have learned so far, it's : https://exercism.org/ .
I will try to do some exercise a write about it
Today i have done :
JavaScript - Lucian's Luscious Lasagna , it's about creating function and variable

PS:

  • if you see any spelling or grammar mistakes, can you notify me (I am still improving my English) ? thank you !
  • if you have any tips for improving my post feel free to comment on the po

Top comments (0)