Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const arr = ['a', 'b', 'c']
- // console.log(arr.length)
- //access by index O(1)
- // console.log(arr[arr.length - 1])
- //adding item/element at the end of the array O(1)
- // arr[arr.length] = 'd'
- // arr.push('d')
- // //removing element from end of the array time complexity O(1)
- // arr.pop()
- //at the beginning of array adding element O(n)
- // arr.unshift('z')
- //at the beginning of array removing element O(n)
- // arr.shift('z')
- //finding element from an array O(n)
- // for(let elm of arr){
- // if(elm === 'c') console.log(true)
- // }
- //forEach, map, filter, reduce, slice, splice O(n)
- // console.log(arr)
- //Object
- const obj = {
- name: 'samim',
- email: 'samimfazlu@gmail.com',
- }
- //accessing element O(1)
- console.log(obj.name)
- //adding/inserting element O(1)
- obj.profession = 'programmer'
- //delete/removal element O(1)
- delete obj.profession
- console.log(obj)
- //searching O(1)
- console.log('email' in obj)
- //searching O(n)
- // for(let key in obj){
- // console.log(obj[key])
- // console.log(key)
- // }
- //Object.keys() O(n)
- // console.log(Object.keys(obj))
- // //Objet.values() O(n)
- // console.log(Object.values(obj))
- // //Object.entries() O(n)
- // console.log(Object.entries(obj))
- // console.log(obj.name)
- // console.log(obj["name"])
- // console.log(obj['1email']) //invalid key must be accessed by []
- //when to use array
- //when order is important
- // when you need faster access (by index) or
- //adding or removal(at the end)
- //when to use object
- //when order isn't important
- // when you need faster access or removal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement