How to remove array object cases after switch statement - javascript

I have a menu system in place and I have got some code for the checkout system to identify deals/discounts if certain items show up in the cart.
Here is the code so far:
var shampoo = false;
var dye = false;
var conditioner = false;
for (var i in cartArray) {
switch (cartArray[i].name) {
case 'Pantene Pro-V':
drinks = true;
break;
case 'Red Dye':
chicken = true;
break;
case 'Herbal Conditioner':
kebab = true;
break;
}
if (shampoo && dye && conditioner) {
console.log("yes");
// shoppingCart.removeItemFromCartAll(cartArray[i].name);
// window.location.reload(true);
break;
}
}
if (!shampoo || !dye || !conditioner) {
console.log("no");
}
The console log shows a yes in a full random cart so that's a positive but I tried to remove these three items together as a group but it doesn't work.
Here is the code for my remove function above:
obj.removeItemFromCartAll = function(name) {// removes all item name
for (var i in cart) {
if (cart[i].name === name){
cart.splice(i,1);
break;
}
}
saveCart();
};
I tried inserting this code:
shoppingCart.removeItemFromCartAll(cartArray[i].name);
Underneath each case but it ends up removing the item individually in the cart, not as a group of three.
Here is what the console looks like (array object):
yes
(4) [{…}, {…}, {…}, {…}]
0: {name: "Pantene Pro-V", price: 2.5, count: 1, total:
"2.50"}
1: {name: "Red Dye", price: 4, count: 1, total: "4.00"}
2: {name: "deodorant", price: 2.5, count: 1, total: "2.50"}
3: {name: "Herbal Conditioner", price: 1, count: 1, total: "1.00"}
length: 4
__proto__: Array(0)
Please help. Thank you.
Btw in the end I want to remove these three and add another item into the cart from the menu (like a package that has these three at a reduced price...FYI).
EDIT - commenting out this part here
shoppingCart.removeItemFromCartAll(cartArray[i].name);
removes the last item in the cart and substituting splice method for delete removes the whole cart and breaks the cart system

As I understand this code, it correctly removes only one item
cart.splice(i,1);
Here you provided 1 which means it will remove 1 item forward from the index provided as first argument. Try to change it to 3.
Worse case scenario would be if you have rumbled array and those 3 items are not after each other. Then at best you should make some property which would assign items to specific group and then use array's filter method to filter out items with group you want to remove

Use delete keyword
obj.removeItemFromCartAll = function(name) {// removes all item name
for (var item in cart) {
if (cart[item].name === name){
delete cart[item]
break;
}
}
saveCart();
};

"Underneath each case but it ends up removing the item individually in the cart, not as a group of three."
A group of three or three individual items is not important, what's important is that they are removed. Situations such as a customer having less than 3 sale items or none at all and/or not in the same order should be considered. A switch acting as a filter is a bad idea, you want to avoid hardcoding dynamic data (I'm assuming that the items in the cart would be different for each customer and that the sale items eventually change as well.)
Demo Outline
list An Array of Objects representing the contents of a customer's cart.
sale An Array of Objects representing the items currently on sale.
Convert both arrays into Maps.
var discount = convertToMap(list, "name")
var customer = convertToMap(sale, "name")
Compare discount and customer Maps to each other and merge each matching key/value pair from discount to customer.
Result is a Map with this pattern:
`[["Muffin Mix - Raisin Bran", {"name":"Muffin Mix - Raisin Bran","price":5.85,"qty":1,"total":2.92,"saved":2.93}], ...]`
Demo
// Sale Items - Note the "total" and "saved" values
let sale = [
{"name":"Flavoring - Orange","price":4.57,"qty":1,"total":2.00,"saved":2.57},
{"name":"Muffin Mix - Raisin Bran","price":5.85,"qty":1,"total":2.92,"saved":2.93},
{"name":"Pepsi - Diet, 355 Ml","price":3.63,"qty":1,"total":1.81,"saved":1.82}
];
// Cart Items - This represents the customer's cart
let cart = [
{"name":"Plastic Arrow Stir Stick","price":0.75,"qty":1,"total":0.75,"saved":0},
{"name":"Veal - Round, Eye Of","price":22.91,"qty":1,"total":22.91,"saved":0},
{"name":"Calypso - Pineapple Passion","price":8.69,"qty":1,"total":8.69,"saved":0},
{"name":"Sloe Gin - Mcguinness","price":33.27,"qty":1,"total":33.27,"saved":0},{"name":"Wine - Sake","price":28.47,"qty":1,"total":28.47,"saved":0},
{"name":"Dried Figs","price":1.78,"qty":1,"total":1.78,"saved":0},
{"name":"Pepsi - Diet, 355 Ml","price":3.63,"qty":1,"total":3.63,"saved":0},
{"name":"Olives - Moroccan Dried","price":15.17,"qty":1,"total":15.17,"saved":0},
{"name":"Muffin Mix - Raisin Bran","price":5.85,"qty":1,"total":5.85,"saved":0}
];
/*
# convertToMap(array, key)
# Params: array [Array of Objects]..(ex. list)
# key [String]..............(ex. "name")
# Converts an Array of Objects to a MAP
# iNPUT: [{key,...}, {key,...}, {key,...}], key
# ~~~~~~~~~~~~~~===~~~~~~~~~~~~~~~
# OUTPUT: [[key, {key,...}], [key, {key,...}], [key, {key,...}]]
*/
const convertToMap = (array, key) => {return new Map(array.map(item => [item[key], item]));};
// Get a Map of sale
const discount = convertToMap(sale, 'name');
// Get a Map of cart
const customer = convertToMap(cart, 'name');
/*
# mergeMaps(mapA, mapB)
# Params: mapA [Map]...smaller Map...(ex. [[...], [...]])
# mapB [Map]...larger Map....(ex. [[...], [...]])
# Compares MapA to MapB
# If any matches by value occur,
# replace MapB key/value with MapA key/value
*/
const mergeMaps = (mapA, mapB) => {
for (let key of mapB.keys()) {
if (mapA.has(key)) {
mapB.set(key, mapA.get(key));
}
}
return mapB;
};
let shoppingA = mergeMaps(discount, customer);
console.log(shoppingA.get("Sloe Gin - Mcguinness"));
console.log("~~~~~~~~~~~~~~~~~~~~~-====-~~~~~~~~~~~~~~~~~~~~~");
// [OPTION] Convert new Map to a 2D Array
let shoppingB = Array.from(mergeMaps(discount, customer));
console.log(shoppingB);

Related

looping over objects javascript

So recently I learned about using for in loops to loop over objects. We were given this problem to solve regarding a basic cart object that contains name of object and then quantity and price.
const cart = {
"Gold Round Sunglasses": { quantity: 1, priceInCents: 1000 },
"Pink Bucket Hat": { quantity: 2, priceInCents: 1260 },
};
We have to write 2 functions, one that calculates total cost of inventory in cents and the other displays the inventory.
The calculateCartTotal function will take in the cart and return a total price, in cents, of everything inside of it.
The printCartInventory function will take in the cart and return a string, joined by \n, of the quantity and name of each item.
I was able to the finish question one with ease but am struggling with the 2nd one.
1st function:
function calculateCartTotal(cart) {
let total = 0;
for(let item in cart){
const product = cart[item]
const quantity = product.quantity
const price = product.priceInCents
total += quantity * price
}
return total
}
2nd function:
function printCartInventory(cart) {
let inventory = ""
for(let item in cart){
const product = cart[item]
const quantity = product.quantity
inventory += `${quantity}x${product}/n`
}
return inventory
}
When I test the 2nd function the autograder gives this error:
expected '2x[object Object]/n1x[object Object]/n1x[object Object]/n3x[object Object]/n' to include '2xCanvas Tote Bag\n1xBlack and White Chuck On Dress\n1xNatural Straw Wide Brim Hat\n3xBlue Stripe Casual Shirt'
When you look at the error message, note the part that says [object Object]. This is part of your code's output, and should ring a bell. It means your code tries to put an object in a string, instead of a string.
The guilty code is here:
inventory += `${quantity}x${product}/n`
product is not a string, but an object. It is not what you intended to output there. What you want to output is the name of the product, which is the key, not the value associated with that key. So it should be:
inventory += `${quantity}x${item}/n`

Return new Object that includes filtered nested array

I basically have a menu system where the user clicks on the store name and it displays information for that store.
That works.
Now, I also have a filter panel where the user can choose to sort the menu list based on the filter button clicked. I can't for the life of me figure this out.
How do I use array functions to create an array that returns a new object that also has updated menus?
burgers.forEach(function(e){
e.menu.filter (el => el.itemPrice < 3).map(el => {
if(el.itemPrice < 3){
console.log(el);
}
});
})
When I log (e) it all burger stores menu item that is less than 5. I want to then create a new object that has all the information from the original but the menu section should be modified to only show the items less than $5.
Object:
let burgers = [
{
"storeSpace": "Open Space",
"storeName": "Mcdonalds",
"menu": [
{
"itemImage": "https://www.mcdonalds.com/is/image/content/dam/ca/nfl/web/nutrition/products/header/en/mcdonalds-hamburger.jpg?$Product_Desktop$",
"itemName": "Hamburger",
"itemPrice": 2.99,
},
{
"itemImage": "https://www.mcdonalds.com/is/image/content/dam/ca/nfl/web/nutrition/products/header/en/mcdonalds-hamburger.jpg?$Product_Desktop$",
"itemName": "Hamburger",
"itemPrice": 3.99,
},
{
"itemImage": "https://www.mcdonalds.com/is/image/content/dam/ca/nfl/web/nutrition/products/header/en/mcdonalds-hamburger.jpg?$Product_Desktop$",
"itemName": "Hamburger",
"itemPrice": 6.99,
},
]
},
]
If I call the object again, it does include the items that are less than $5 but it also shows all the other menu items which i don't want. It should only show the first 2 but it returns the whole list of items.
Here is an image: https://i.imgur.com/jOHkB6a.png
As you see, that time I set the max amount to $3 and only 1 item in the object is under $3 but it returns the whole menu list. That is why I need to somehow update the current menu item.
Here is code:
const newBurgersMenu = burgers.map(function(burger){
const newMenu = burger.menu.filter(function(item){
if(item.itemPrice < 5){
return item
}
})
return newMenu;
})
// First way to create new burgers
const newBurgersObj = [{...burgers[0], menu:newBurgersMenu[0]}]
// OR Second way to create new burgers
const newBurgers = burgers.slice()
newBurgers[0].menu = newBurgersMenu
console.log({newBurgersObj})
console.log({newBurgers})
Is this what you are looking for. Also link to jsfiddle https://jsfiddle.net/vrheL4tw/1/
Hope I answered your question

JavaScript: How to remove the duplicate entries from array with respect to multiple keys

I have an array like this:
0: {ClusterId: "10.210.28.231", Device IP: "10.210.28.231", SlotID: "1"}
1: {ClusterId: "10.210.28.234", Device IP: "10.210.28.234", SlotID: "1"}
2: {ClusterId: "10.210.28.231", Device IP: "10.210.28.231", SlotID: "2"}
3: {ClusterId: "10.210.28.234", Device IP: "10.210.28.234", SlotID: "2"}
4: {ClusterId: "10.210.28.234", Device IP: "10.210.28.234", SlotID: "2"}
5: {ClusterId: "10.210.28.231", Device IP: "10.210.28.231", SlotID: "2"}
I am trying to remove the duplicates by both ClusterId and SlotID. Below is my sample code where I am pushing the duplicate values into one array (duplicateArrayVal) and non-duplicates to other array (importArrayVal)
UniqueArraybyObject($scope.excelRowArray, "SlotID", "ClusterId");
function UniqueArraybyObject(collection, slotId, clusterName) {
var importArrayVal = [],
slot = [],
clusters = [];
$scope.duplicateArrayVal = [];
angular.forEach(collection, function(item) {
var tempSlot = item[slotId];
var cluster = item[clusterName];
if ((slot.indexOf(tempSlot) === -1) || (clusters.indexOf(cluster) === -1)) {
slot.push(tempSlot);
clusters.push(cluster);
importArrayVal.push(item);
} else {
$scope.duplicateArrayVal.push(item);
}
});
return importArrayVal;
};
The problem that occurs for me is items at 3,4 and 5 rows are not displaying as these are considered as duplicates. But only the 4 and 5th row will be the duplicate one and not the 3th. I need the 3th row to be in non-duplicate array.
Please help me to achieve this
Once solution would be to construct an intermediate map where:
the key of map is a compound key from the ClusterId and SlotID values of each item in the input array and
the value is the last item encountered in the input array for that key
After building a map in this way, you'll have a set of values that are unique by the ClusterId and SlotID item values. The resulting map can then be passed to Object.values() to obtain the unique item values as an array:
const data=[
{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"1",'Actions':"Assign"},
{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"1",'Actions':"Assign"},
{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"2",'Actions':"Assign"},
{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"2",'Actions':"Assign"},
{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"2",'Actions':"Assign"},
{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"2",'Actions':"Assign"}
];
/* Reduce input array to a map (see following description), and then
extract map values as array to obtain final result */
const result = Object.values(data.reduce((map, item) => {
/* Build a map of values where with a compound key derived from the
ClusterId and SlodID field values */
return { ...map,
[`${ item.ClusterId }-${ item.SlotID }`]: item
};
}, {}))
console.log(result);
Update
If compatibility with older browsers is a requirement, the following will achieve the same result without dependence on newer ES6 syntax:
var data=[
{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"1",'Actions':"Assign"},{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"1",'Actions':"Assign"},{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"2",'Actions':"Assign"},{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"2",'Actions':"Assign"},{'ClusterId':"10.210.28.234",'Device IP':"10.210.28.234",'SlotID':"2",'Actions':"Assign"},{'ClusterId':"10.210.28.231",'Device IP':"10.210.28.231",'SlotID':"2",'Actions':"Assign"}
];
var map = {};
for(var i = 0; i < data.length; i++) {
var item = data[i];
map[item.ClusterId + "-" + item.SlotID] = item;
}
var result = [];
for(var k in map) {
result.push(map[k]);
}
console.log(result);

How to find duplicates, count elements in array using javascript/jquery

I have a form in HTML. In this form, I can build catalog preferences.
When I'm done with that I can click on save button and build a next one. The result is saving in an array.
When I have more than one saved catalog(sku=stock keeping unit) in my array it's possible that there are some duplicates. How can I remove the duplicates and count them (The quantity should stay even if there is a duplicate)?
Jsfiddle
Array(Array(
0:5016s18gercol, //sku(stock keeping unit)
1: 100, //quantity
2: 5017ext10002, //extra sku for extra costs(cataloge in color)
3: 1
),
Array(
0:5016s43gerbw, //sku
1: 100, //quantity
2: 5017ext10001,//extra sku for extra costs(catalog own cover)
3: 1 //quanitity extra costs
),
Array(
0: "5016s43gercol" //sku
1: "400" //quantity
2:"5017ext10001" //extra sku (own cover)
3:"1" //quantity sku
4:"5017ext10002" //extra sku (in color)
5:"1" //quantity sku
)
)
This is what i get. It should look like this:
array(5016s18gercol,
500,
5017ext10002,
2,
5017ext10001,
2)
Every sku is unique so i cant have two same sku's. I have to count them if there are more than one.
Use array.prototype.map and array.prototype.some:
var values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName4' },
{ name: 'someName2' }
];
var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});
console.log(isDuplicate);

Find separation values from a starting node

I found on some online coding exercises and this one looks really cool and I wanted to give it a shot.
Problem Statement
Quinn is a pretty popular, and extremely modest guy. Other students measure their popularity in a unit called QDist.
One can calculate their QDist value by finding the degrees of separation between their self and Quinn. For example:
If Quinn is friends with Dave, and Dave is friends with Travis, then Dave's QDist value is 1, and Travis is 2.
Output
name QDist for each person entered ordered alphabetically by name.
In the event that a person is not connected to Quinn in anyway, output name uncool
Given a list of friendships, list each person and their QDist value in alphabetical order.
Sample Input/output
10
Alden Toshiko
Che Kortney
Che Dorian
Ronda Lindell
Sharon Alden
Dorian Quinn
Owen Sydnee
Alden Che
Dorian Tyra
Quinn Ally
Output
Alden 3
Che 2
Dorian 1
Kortney 3
Lindell uncool
Ally 1
Owen uncool
Quinn 0
Ronda uncool
Sharon 4
Sydnee uncool
Toshiko 4
Tyra 2
My Approach
Firstly, I don't want the answer I just want a hint or some guidance on how I should approach the problem in javascript (as its the language i'm the most familiar with). My thought was to break the program into an object and arrays, and try to create a family relationship between each name, sort of as a nested object or perhaps an array. Then I could use some sort of recursion to find how deep the array or object goes.
What would be the best approach?
From the input you could create a list of persons. It could be an object, where each key is a person's name, and the corresponding value is an array of names, representing the friends of that person. Of course you should make sure that when you add B as a friend of A, you must also add A as a friend of B.
For the example input, the above structure would look like this:
{
"Alden": ["Toshiko","Sharon","Che"],
"Toshiko": ["Alden"],
"Che": ["Kortney","Dorian","Alden"],
"Kortney": ["Che"],
"Dorian": ["Che","Quinn","Tyra"],
"Ronda": ["Lindell"],
"Lindell": ["Ronda"],
"Sharon": ["Alden"],
"Quinn": ["Dorian","Ally"],
"Owen": ["Sydnee"],
"Sydnee": ["Owen"],
"Tyra": ["Dorian"],
"Ally": ["Quinn"]
}
Then keep track of a list of names, starting with just Quinn, and also a distance, starting at 0.
Then for each name in that list, assign the current distance as their QDist value. Then find their friends and put them all together. Remove names that have already received a QDist value.
Then increase the distance, and repeat the above for that new list of names.
Keep repeating until the list of names is empty.
Note that if you do things in the right order, you can replace a persons list of friends by the QDist value. So the above structure would change after the first two iterations to:
{
"Alden": ["Toshiko","Sharon","Che"],
"Toshiko": ["Alden"],
"Che": ["Kortney","Dorian","Alden"],
"Kortney": ["Che"],
"Dorian": 1,
"Ronda": ["Lindell"],
"Lindell": ["Ronda"],
"Sharon": ["Alden"],
"Quinn": 0,
"Owen": ["Sydnee"],
"Sydnee": ["Owen"],
"Tyra": ["Dorian"],
"Ally": 1
}
When the algorithm finishes, you have:
{
"Alden": 3,
"Toshiko": 4,
"Che": 2,
"Kortney": 3,
"Dorian": 1,
"Ronda": ["Lindell"],
"Lindell": ["Ronda"],
"Sharon": 4,
"Quinn": 0,
"Owen": ["Sydnee"],
"Sydnee": ["Owen"],
"Tyra": 2,
"Ally": 1
}
Now the remaining friends arrays need to be replaced with "uncool", as apparently the corresponding people have no connection with Quinn. Also the list needs to be sorted.
Spoiler warning!
Here is a working snippet:
// Get input as text
var input = `10
Alden Toshiko
Che Kortney
Che Dorian
Ronda Lindell
Sharon Alden
Dorian Quinn
Owen Sydnee
Alden Che
Dorian Tyra
Quinn Ally`;
// Build persons list with friends list
var persons =
// Take the input string
input
// Split it by any white-space to get array of words
.split(/\s+/)
// Skip the word at position 0: we don't need the line count
.slice(1)
// Loop over that array and build an object from it
.reduce(
// Arguments: obj = result from previous iteration
// name = current name in names array
// i = index in that array
// names = the whole array being looped over
(obj, name, i, names) => (
// Get the list of friends we already collected for this name.
// Create it as an empty array if not yet present.
obj[name] = (obj[name] || [])
// Add to that list the previous/next name, depending
// whether we are at an odd or even position in the names array
.concat([names[i%2 ? i-1 : i+1]])
// Use the updated object as return value for this iteration
, obj)
// Start the above loop with an empty object
, {});
// Now we have a nice object structure:
// { [name1]: [friendName1,friendName2,...], [name2]: ... }
// Start with Quinn as the only person we currently look at.
var friends = ['Quinn'];
// Increment the distance for each "generation" of friends
// until there are none left.
for (var i = 0; friends.length; i++) {
// Replace the friends list with a new list,
// while giving the friends in the current list a distance
friends =
// Start with the current list of friends
friends
// Loop over these friends.
// Only keep those that still have a friends array (object) assigned to them,
// since the others were already assigned a distance number.
.filter(friend => typeof persons[friend] === "object")
// Loop over those friends again, building a new list of friends
.reduce((friends, friend, k) => [
// Add this friends' friends to the new list
friends.concat(persons[friend]),
// ... and then replace this friends' friend list
// by the current distance we are at.
persons[friend] = i
// Return the first of the above two results (the new list)
// for the next iteration.
][0]
// Start with an empty array for the new friends list
, []);
}
// Now we have for each person that connects to Quinn a number:
// { [name1]: number, ... }
// Convert this to a format suitable to output
var result =
// Get list of names from the object (they are the keys)
Object.keys(persons)
// Sort that list of names
.sort()
// Loop over these names to format them
.map(name =>
// Format as "name: distance" or "name: uncool" depending on whether there
// still is an array of friends (object) in this entry
name + ': ' + (typeof persons[name] == 'object' ? 'uncool' : persons[name]));
// Output the result in the console
console.log(result);
And a more verbose, but easier to understand version:
// Get input as text
var input = `10
Alden Toshiko
Che Kortney
Che Dorian
Ronda Lindell
Sharon Alden
Dorian Quinn
Owen Sydnee
Alden Che
Dorian Tyra
Quinn Ally`;
// Build persons list with friends list
// Take the input string
var persons = input;
// Split it by any white-space to get array of words
persons = persons.split(/\s+/)
// Skip the word at position 0: we don't need the line count
persons = persons.slice(1)
// Loop over that array and build an object from it
var obj = {}; // Start loop with an empty object
for (var i = 0; i < persons.length; i++) {
var name = persons[i]; // name = current name in names array
// Get the list of friends we already collected for this name.
// Create it as an empty array if not yet present.
if (obj[name] === undefined) obj[name] = [];
// Add to that list the previous/next name, depending
// whether we are at an odd or even position in the names array
obj[name].push(persons[i%2 === 1 ? i-1 : i+1]);
}
// Assign result to persons
persons = obj;
// Now we have a nice object structure:
// { [name1]: [friendName1,friendName2,...], [name2]: ... }
// Start with Quinn as the only person we currently look at.
var friends = ['Quinn'];
// Increment the distance for each "generation" of friends
// until there are none left.
for (var i = 0; friends.length !== 0; i++) {
// Loop over those friends, building a new list of friends
// Start with an empty array for the new friends list
var newFriends = [];
for (var k = 0; k < friends.length; k++) {
var friend = friends[k];
// Only consider those that still have a friends array (object) assigned to them,
// since the others were already assigned a distance number.
if (typeof persons[friend] === "object") {
// Add this friends' friends to the new list
newFriends = newFriends.concat(persons[friend]);
// ... and then replace this friends' friend list
// by the current distance we are at.
persons[friend] = i;
}
};
// Make the new list the current list:
friends = newFriends;
}
// Now we have for each person that connects to Quinn a number:
// { [name1]: number, ... }
// Convert this to a format suitable to output
// Get list of names from the object (they are the keys)
var result = Object.keys(persons);
// Sort that list of names
result.sort();
// Loop over these names to format them
for (var i = 0; i < result.length; i++) {
var name = result[i];
// Format as "name: distance" or "name: uncool" depending on whether there
// still is an array of friends (object) in this entry
if (typeof persons[name] == 'object') {
result[i] = name + ': uncool';
} else {
result[i] = name + ': ' + persons[name];
}
}
// Output the result in the console
console.log(result);
If i had to solve this problem,
First I would create an array and initialise it with student who are 1 with Quinn by finding rows (elements) studentX ←→ Quinn in original array.
Then I would search recursively those who are level n with quinn by finding rows studentX ←→ student(n-1)FromQuinn
My attempt to understand
var persons = input.split(/\s+/).slice(1).reduce(function(obj,name,i,names){
return (obj[name] = (obj[name] || []).concat([names[i%2 ? i-1 : i+1]]), obj);
},{});
First input.split(/\s+/).slice(1) Gives us an array with all of the names in it.
Now
(obj[name] = (obj[name] || []).concat([names[i%2 ? i-1 : i+1]]), obj);
obj is set by default to due to {} according to the reduce method property.
name is current value which is basically going from Alden all the way to Ally. i will go from 1-10 and names is the array
Now we are saying set obj[name] = obj[name].concat([names[i%2 ? i-1 : i+1]]),obj); IF this is possible. If this isn't possible set obj[name] = [].concat([names[i%2 ? i-1 : i+1]]),obj);. This is my interpretation from reading up on ||
Example iteration
first obj = {}, and name will be Alden
so the type Alden i.e persons = { Alden: ..} will be obj[Alden].concat(names[2],obj), it will be 2, since 1 mod 2 doesn't get reached.
Now there is where I am a bit confused... what exactly is the ,obj doing here..? am I interpreting this right?

Categories

Resources