JavaScript simple Array and Object manipulations
JavaScript is Very very fun actually! Look below :D
(1) Removing an element from an array
(2) Adding an element to an array
(3) Replacing an element in an array
let arr = ['java', 'python', 'ruby'];
let removing_element = arr.filter(element => element !== 'java')
console.log(removing_element);
let adding_element = [...arr, 'javascript']
console.log(adding_element);
let replacing_element = arr.map(element=>element === 'java'?'javascript':element)
console.log(replacing_element);
["python", "ruby"]
["java", "python", "ruby", "javascript"]
["javascript", "python", "ruby"]
(4) Removing an entry in an object
(5) Adding an entry to an object
(6) Updating an entry in an object
let state = {name:'harry', max_pullups:'21'}
let removing_value = {...state, name: undefined}
console.log(removing_value);
let removing_value = _.omit(state, 'name') //this can be done with lodash!
let adding_new_entry = {...state, age:'99'}
console.log(adding_new_entry);
let updating_entry = {...state, name:'tlonist'}
console.log(updating_entry);
{name: undefined, max_pullups: "21"}
{name: "tlonist", max_pullups: "21"}
{name: "harry", max_pullups: "21", age: "99"}
(7) Iterating keys in object
(8) Iterating values in object
(8) Iterating entries in object
let state = {'name':'alex Delarge', age:22, createdby:'anthony burgess'}
let arr_keys = Object.keys(state)
console.log(arr_keys);
let arr_values = Object.values(state)
console.log(arr_values);
let arr_entry = Object.entries(state)
console.log(arr_entry);
["name", "age", "createdby"]
["alex Delarge", 22, "anthony burgess"]
[Array(2), Array(2), Array(2)]
https://medium.com/@darrion/ways-to-loop-through-an-object-in-javascript-622353049c7f