Last Sunday, my mom asked me to get biryani ingredients for 10 guests.
Below are the ingredients
Meat
Basmati Rice
Cardamom
Saffron water
Ghee
Milk
Corriander
eggs
Let’s store the ingrediants in an array.
Ingredients = ["Basmati Rice","Cardamom","Saffron water","Ghee","Milk","Corriander","eggs","Meat"]
To know the best place to get these ingredients are available.
I will be using a method called indexOf()
in Javascript.
Indexof()
The indexOf()
method of Array
instances returns the first index at which a given element can be found in the array, or -1 if it is not present.
Syntax:
indexOf(searchElement)
indexOf(searchElement, fromIndex)
Ingredients = ["Basmati Rice","Cardamom","Saffron water","Ghee","Milk","Corriander","eggs", "Meat" ]
console.log(beasts.indexOf('Basmati Rice'));
// Expected output: 0
console.log(beasts.indexOf('Meat'));
// Expected output: 7
I eventually found all indrediants at two places so i catogorized accordingly.
Shop_1 = "[Basmati Rice","Cardamom","Saffron water"]
Shop_2 = ["Ghee","Milk","Corriander","Pudina(Mint)","eggs", "Meat"]
Mom told that all the guests are vegetarians.
Now we will use filter()
function to do that.
filter()
The filter()
method of Array
instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
Syntax:
filter(callbackFn)
filter(callbackFn, thisArg)
let Shop_2 = ["Ghee", "Milk", "Corriander", "eggs", "Meat"];
Shop_2 = Shop_2.filter(item => item !== "Meat");
console.log(Shop_2);
// Expected output : > Array ["Ghee", "Milk", "Corriander", "eggs"]
Now that meat is out of the bag but mon said no eggs also.
Nowe we will use pop()
function to do that
pop()
The pop()
method of Array
instances removes the last element from an array and returns that element. This method changes the length of the array.
Syntax:
pop()
Shop_2 = ["Ghee", "Milk", "Corriander", "eggs"];
console.log(Shop_2.pop());
// Expected output: "eggs"
console.log(Shop_2)
// Expected output: "Ghee", "Milk", "Corriander"
Now mon told me that without raw jack fruit vegetable biryani dosen’t tast good.
Let’s use push()
function to do that.
push()
The push()
method of Array
instances adds the specified elements to the end of an array and returns the new length of the array.
Syntax:
push()
push(element1)
push(element1, element2)
push(element1, element2, /* …, */ elementN)
Shop_2 = ["Ghee", "Milk", "Corriander"];
count = Shop_2.push('Jack fruit');
console.log(count);
// Expected output: 4
console.log(Shop_2);
// Expected output: "Ghee", "Milk", "Corriander", 'Jack fruit'
Shop_2.push("Ghee", "Milk", "Corriander");
console.log(Shop_2);
// Expected output: Array [Ghee", "Milk", "Corriander", "Jack fruit"]
Mom called me there already Ghee available in home but need more Milk.
Let’s add milk with the method copyWithin()
.
copyWithin()
The copyWithin()
method of Array
instances shallow copies part of this array to another location in the same array and returns this array without modifying its length.
Syntax:
copyWithin(target, start)
copyWithin(target, start, end)
Shop_2 = ["Ghee", "Milk", "Corriander", "Jack fruit"];
// Copy to index 0 the element at index 1
console.log(array1.copyWithin(0, 1, 3));
// Expected output: Array ["Milk", "Milk", "Corriander", "Jack fruit"];
Now combine two bags from both market in to one.
We will use concat()
function for that
concat()
The concat()
method of Array
instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Syntax:
concat()
concat(value1)
Shop_1 = ["Basmati Rice","Cardamom","Saffron water"]
Shop_2 = ["Milk", "Milk", "Corriander", "Jack fruit"];
final_ingredients = Shop_1.concat(Shop_2);
console.log(final_ingredients);
// Expected output: Array ["Basmati Rice","Cardamom","Saffron water","Milk", "Milk", "Corriander", "Jack fruit"]
Now Mom wants to all ingrediants one by one.
Lets do that with method values()
values()
The values()
method of Array
instances returns a new array iterator object that iterates the value of each item in the array.
Syntax:
values()
const final_ingredients = ["Basmati Rice","Cardamom","Saffron water","Milk", "Milk", "Corriander", "Jack fruit"]
const iterator = final_ingredients.values();
for (const value of iterator) {
console.log(value);
}
// Expected output: "Basmati Rice"
// Expected output: "Cardamom"
// Expected output: "Saffron water"
// Expected output: "Milk"
// Expected output: "Milk"
// Expected output: "Corriander"
// Expected output: "Jack fruit"