xxxxxxxxxx
addItem = item => {
this.setState({
emp: [
this.state.emp,
item
]
})
}
xxxxxxxxxx
const arr = [1, 2, 3, 4];
arr.push(5);
console.log(arr); // [1, 2, 3, 4, 5]
// another way
let arr = [1, 2, 3, 4];
arr = [arr, 5];
console.log(arr); // [1, 2, 3, 4, 5]
xxxxxxxxxx
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
xxxxxxxxxx
// use of .push() on an array
// define array
let numbers = [1, 2]
// using .push()
numbers.push(3) // adds the value 3 to the end of the list
console.log(numbers) // [1, 2, 3]
xxxxxxxxxx
let dailyActivities = ['eat', 'sleep'];
// add an element at the end
dailyActivities.push('exercise');
console.log(dailyActivities); // ['eat', 'sleep', 'exercise']
xxxxxxxxxx
var s = new Set();
// Adding alues
s.add('hello');
s.add('world');
s.add('hello'); // already exists
// Removing values
s.delete('world');
var array = Array.from(s);
xxxxxxxxxx
let array = ["Chicago", "Los Angeles", "Calgary", "Seattle", ]
// print the array
console.log(array)
//Adding a new item in an array without touching the actual array
array.push('New Item') < variable_name > .push( < What you want to add > )
//How many items does the array have?
console.log("This array has", array.length, "things in it")
xxxxxxxxxx
#include <vector>
#include <iostream>
int main() {
std::vector<int> v;
v.push_back(42);
std::cout << v.size() << "\n";
std::cout << v.back() << "\n";
}
Output:
1
42
xxxxxxxxxx
addItems = items => {
this.setState({
emp: [
this.state.emp,
items
]
})
}