PeriodicTable search To Perodify a Name. (Java script) - javascript
I'm trying to search in a array which contains PeriodicElements to Perodify a given name. This link
http://www.lmntology.com/ is what exactly what I want to do. But I can't find tune the search to
return Elements with two letters, Basically it returns the first match.
For example
"CARL" the generic search would return - Carbon, Adamantium, Rearden, Latinum
where as "CARL" should also return - Carbon, Argon ,Latinum
Any suggestions? Or a fiddle would be greatly appreciated.
Fiddle: http://jsfiddle.net/9vxGL/12/
var table = [
"H", "Hydrogen", "1.00794", 1, 1,
"He", "Helium", "4.002602", 18, 1,
"Li", "Lithium", "6.941", 1, 2,
"Be", "Beryllium", "9.012182", 2, 2,
"B", "Boron", "10.811", 13, 2,
"C", "Carbon", "12.0107", 14, 2,
"N", "Nitrogen", "14.0067", 15, 2,
"O", "Oxygen", "15.9994", 16, 2,
"F", "Fluorine", "18.9984032", 17, 2,
"Ne", "Neon", "20.1797", 18, 2,
"Na", "Sodium", "22.98976...", 1, 3,
"Mg", "Magnesium", "24.305", 2, 3,
"Al", "Aluminium", "26.9815386", 13, 3,
"Si", "Silicon", "28.0855", 14, 3,
"P", "Phosphorus", "30.973762", 15, 3,
"S", "Sulfur", "32.065", 16, 3,
"Cl", "Chlorine", "35.453", 17, 3,
"Ar", "Argon", "39.948", 18, 3,
"K", "Potassium", "39.948", 1, 4,
"Ca", "Calcium", "40.078", 2, 4,
"Sc", "Scandium", "44.955912", 3, 4,
"Ti", "Titanium", "47.867", 4, 4,
"V", "Vanadium", "50.9415", 5, 4,
"Cr", "Chromium", "51.9961", 6, 4,
"Mn", "Manganese", "54.938045", 7, 4,
"Fe", "Iron", "55.845", 8, 4,
"Co", "Cobalt", "58.933195", 9, 4,
"Ni", "Nickel", "58.6934", 10, 4,
"Cu", "Copper", "63.546", 11, 4,
"Zn", "Zinc", "65.38", 12, 4,
"Ga", "Gallium", "69.723", 13, 4,
"Ge", "Germanium", "72.63", 14, 4,
"As", "Arsenic", "74.9216", 15, 4,
"Se", "Selenium", "78.96", 16, 4,
"Br", "Bromine", "79.904", 17, 4,
"Kr", "Krypton", "83.798", 18, 4,
"Rb", "Rubidium", "85.4678", 1, 5,
"Sr", "Strontium", "87.62", 2, 5,
"Y", "Yttrium", "88.90585", 3, 5,
"Zr", "Zirconium", "91.224", 4, 5,
"Nb", "Niobium", "92.90628", 5, 5,
"Mo", "Molybdenum", "95.96", 6, 5,
"Tc", "Technetium", "(98)", 7, 5,
"Ru", "Ruthenium", "101.07", 8, 5,
"Rh", "Rhodium", "102.9055", 9, 5,
"Pd", "Palladium", "106.42", 10, 5,
"Ag", "Silver", "107.8682", 11, 5,
"Cd", "Cadmium", "112.411", 12, 5,
"In", "Indium", "114.818", 13, 5,
"Sn", "Tin", "118.71", 14, 5,
"Sb", "Antimony", "121.76", 15, 5,
"Te", "Tellurium", "127.6", 16, 5,
"I", "Iodine", "126.90447", 17, 5,
"Xe", "Xenon", "131.293", 18, 5,
"Cs", "Caesium", "132.9054", 1, 6,
"Ba", "Barium", "132.9054", 2, 6,
"La", "Lanthanum", "138.90547", 4, 9,
"Ce", "Cerium", "140.116", 5, 9,
"Pr", "Praseodymium", "140.90765", 6, 9,
"Nd", "Neodymium", "144.242", 7, 9,
"Pm", "Promethium", "(145)", 8, 9,
"Sm", "Samarium", "150.36", 9, 9,
"Eu", "Europium", "151.964", 10, 9,
"Gd", "Gadolinium", "157.25", 11, 9,
"Tb", "Terbium", "158.92535", 12, 9,
"Dy", "Dysprosium", "162.5", 13, 9,
"Ho", "Holmium", "164.93032", 14, 9,
"Er", "Erbium", "167.259", 15, 9,
"Tm", "Thulium", "168.93421", 16, 9,
"Yb", "Ytterbium", "173.054", 17, 9,
"Lu", "Lutetium", "174.9668", 18, 9,
"Hf", "Hafnium", "178.49", 4, 6,
"Ta", "Tantalum", "180.94788", 5, 6,
"W", "Tungsten", "183.84", 6, 6,
"Re", "Rhenium", "186.207", 7, 6,
"Os", "Osmium", "190.23", 8, 6,
"Ir", "Iridium", "192.217", 9, 6,
"Pt", "Platinum", "195.084", 10, 6,
"Au", "Gold", "196.966569", 11, 6,
"Hg", "Mercury", "200.59", 12, 6,
"Tl", "Thallium", "204.3833", 13, 6,
"Pb", "Lead", "207.2", 14, 6,
"Bi", "Bismuth", "208.9804", 15, 6,
"Po", "Polonium", "(209)", 16, 6,
"At", "Astatine", "(210)", 17, 6,
"Rn", "Radon", "(222)", 18, 6,
"Fr", "Francium", "(223)", 1, 7,
"Ra", "Radium", "(226)", 2, 7,
"Ac", "Actinium", "(227)", 4, 10,
"Th", "Thorium", "232.03806", 5, 10,
"Pa", "Protactinium", "231.0588", 6, 10,
"U", "Uranium", "238.02891", 7, 10,
"Np", "Neptunium", "(237)", 8, 10,
"Pu", "Plutonium", "(244)", 9, 10,
"Am", "Americium", "(243)", 10, 10,
"Cm", "Curium", "(247)", 11, 10,
"Bk", "Berkelium", "(247)", 12, 10,
"Cf", "Californium", "(251)", 13, 10,
"Es", "Einstenium", "(252)", 14, 10,
"Fm", "Fermium", "(257)", 15, 10,
"Md", "Mendelevium", "(258)", 16, 10,
"No", "Nobelium", "(259)", 17, 10,
"Lr", "Lawrencium", "(262)", 18, 10,
"Rf", "Rutherfordium", "(267)", 4, 7,
"Db", "Dubnium", "(268)", 5, 7,
"Sg", "Seaborgium", "(271)", 6, 7,
"Bh", "Bohrium", "(272)", 7, 7,
"Hs", "Hassium", "(270)", 8, 7,
"Mt", "Meitnerium", "(276)", 9, 7,
"Ds", "Darmstadium", "(281)", 10, 7,
"Rg", "Roentgenium", "(280)", 11, 7,
"Cn", "Copernicium", "(285)", 12, 7,
"Uut", "Unutrium", "(284)", 13, 7,
"Fl", "Flerovium", "(289)", 14, 7,
"Uup", "Ununpentium", "(288)", 15, 7,
"Lv", "Livermorium", "(293)", 16, 7,
"Uus", "Ununseptium", "(294)", 17, 7,
"Uuo", "Ununoctium", "(294)", 18, 7
];
$("#b").click(function () {
var chars = $('#st').val().split("");
for (var i = 0; i < chars.length; i++) {
search(chars[i]);
}
});
function search (char)
{
for (var i = 0; i < table.length; i++) {
if (table[i] === char) {
console.log(table[i]+" "+table[i+1]+"\n");
}
}
}
This is my initial attempt, It only Matches one Character Case sensitive. I will keep updating this. Please have a look.
Related
APEX/AJAX-CALLBACK/JavaScript: How can i transform a flat JSON Object into nested hierarchy JSON Object to use in D3-Chart
I have the following JavaScript code that calls an AJAX callback. ` pData = { "row": [ { "PARENT_ID": 0, "CHILD_ID": 1, "NAME": "Quality", "LEVEL_A": 0 }, { "PARENT_ID": 1, "CHILD_ID": 2, "NAME": "Machine", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 3, "NAME": "Method", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 4, "NAME": "Material", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 5, "NAME": "Man Power", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 6, "NAME": "Measurement", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 7, "NAME": "Milieu", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 10, "NAME": "Metal Lathe", "LEVEL_A": 1 }, { "PARENT_ID": 2, "CHILD_ID": 8, "NAME": "Mill", "LEVEL_A": 2 }, { "PARENT_ID": 2, "CHILD_ID": 9, "NAME": "Mixer", "LEVEL_A": 2 }, { "PARENT_ID": 4, "CHILD_ID": 11, "NAME": "Masonite", "LEVEL_A": 2 }, { "PARENT_ID": 4, "CHILD_ID": 12, "NAME": "Meat", "LEVEL_A": 2 }, { "PARENT_ID": 4, "CHILD_ID": 13, "NAME": "Marscapone", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 20, "NAME": "Manager", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 21, "NAME": "Magician", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 22, "NAME": "Magister", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 24, "NAME": "Massage Artist", "LEVEL_A": 2 }, { "PARENT_ID": 6, "CHILD_ID": 25, "NAME": "Malleability", "LEVEL_A": 2 }, { "PARENT_ID": 7, "CHILD_ID": 26, "NAME": "Marine", "LEVEL_A": 2 }, { "PARENT_ID": 12, "CHILD_ID": 19, "NAME": "Mutton", "LEVEL_A": 3 }, { "PARENT_ID": 13, "CHILD_ID": 14, "NAME": "Malty", "LEVEL_A": 3 }, { "PARENT_ID": 13, "CHILD_ID": 15, "NAME": "Minty", "LEVEL_A": 3 }, { "PARENT_ID": 14, "CHILD_ID": 17, "NAME": "Minty", "LEVEL_A": 4 }, { "PARENT_ID": 17, "CHILD_ID": 16, "NAME": "spearMint", "LEVEL_A": 5 }, { "PARENT_ID": 17, "CHILD_ID": 18, "NAME": "pepperMint", "LEVEL_A": 5 }, { "PARENT_ID": 22, "CHILD_ID": 23, "NAME": "Malpractice", "LEVEL_A": 3 } ] } function fishbone_sql_ajax_callback() { apex.server.plugin(input_ajax_ID, {//x01: $v('P1_ID') }, { success: function(pData) { //Transform JSON Array into Hierarchy const source = pData.row function tree(data, id, pId) { const [result] = data.reduce( ([r, map], item) => { const d = { ...item, children: []}; const loc = map[item[pId]]; if (loc) { loc.children.push(d); } else { r.push(d); } map[item[id]] = d; return [r, map]; }, [[], {}] ); return result; } console.log(JSON.stringify(tree(source, "CHILD_ID", "PARENT_ID"), null, 4)); console.log(JSON.parse(tree(source, "CHILD_ID", "PARENT_ID"), null, 4)); //json_string = JSON.stringify(tree(source, "CHILD_ID", "PARENT_ID"), null, 4); const js_obj1 = JSON.parse(json_string); console.log(js_obj1) Chart_initialize(js_obj1);}, dataType: 'json' }); } ` How can I turn the flat object into a hierarchy JSON so I can use the JSON object for my D3 chart? JSON output that i want to use ` [ { "PARENT_ID": 0, "CHILD_ID": 1, "NAME": "Quality", "LEVEL_A": 0, "children": [ { "PARENT_ID": 1, "CHILD_ID": 2, "NAME": "Machine", "LEVEL_A": 1, "children": [ { "PARENT_ID": 2, "CHILD_ID": 8, "NAME": "Mill", "LEVEL_A": 2 }, { "PARENT_ID": 2, "CHILD_ID": 9, "NAME": "Mixer", "LEVEL_A": 2 } ] }, { "PARENT_ID": 1, "CHILD_ID": 3, "NAME": "Method", "LEVEL_A": 1 }, { "PARENT_ID": 1, "CHILD_ID": 4, "NAME": "Material", "LEVEL_A": 1, "children": [ { "PARENT_ID": 4, "CHILD_ID": 11, "NAME": "Masonite", "LEVEL_A": 2 }, { "PARENT_ID": 4, "CHILD_ID": 12, "NAME": "Meat", "LEVEL_A": 2, "children": [ { "PARENT_ID": 12, "CHILD_ID": 19, "NAME": "Mutton", "LEVEL_A": 3 } ] }, { "PARENT_ID": 4, "CHILD_ID": 13, "NAME": "Marscapone", "LEVEL_A": 2, "children": [ { "PARENT_ID": 13, "CHILD_ID": 14, "NAME": "Malty", "LEVEL_A": 3, "children": [ { "PARENT_ID": 14, "CHILD_ID": 17, "NAME": "Minty", "LEVEL_A": 4, "children": [ { "PARENT_ID": 17, "CHILD_ID": 16, "NAME": "spearMint", "LEVEL_A": 5 }, { "PARENT_ID": 17, "CHILD_ID": 18, "NAME": "pepperMint", "LEVEL_A": 5 } ] } ] }, { "PARENT_ID": 13, "CHILD_ID": 15, "NAME": "Minty", "LEVEL_A": 3 } ] } ] }, { "PARENT_ID": 1, "CHILD_ID": 5, "NAME": "Man Power", "LEVEL_A": 1, "children": [ { "PARENT_ID": 5, "CHILD_ID": 20, "NAME": "Manager", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 21, "NAME": "Magician", "LEVEL_A": 2 }, { "PARENT_ID": 5, "CHILD_ID": 22, "NAME": "Magister", "LEVEL_A": 2, "children": [ { "PARENT_ID": 22, "CHILD_ID": 23, "NAME": "Malpractice", "LEVEL_A": 3 } ] }, { "PARENT_ID": 5, "CHILD_ID": 24, "NAME": "Massage Artist", "LEVEL_A": 2 } ] }, { "PARENT_ID": 1, "CHILD_ID": 6, "NAME": "Measurement", "LEVEL_A": 1, "children": [ { "PARENT_ID": 6, "CHILD_ID": 25, "NAME": "Malleability", "LEVEL_A": 2 } ] }, { "PARENT_ID": 1, "CHILD_ID": 7, "NAME": "Milieu", "LEVEL_A": 1, "children": [ { "PARENT_ID": 7, "CHILD_ID": 26, "NAME": "Marine", "LEVEL_A": 2 } ] }, { "PARENT_ID": 1, "CHILD_ID": 10, "NAME": "Metal Lathe", "LEVEL_A": 1 } ] } ` I already tried to convert the array (as you can see in my Code), but the problem is that my D3 chart expects a JSON object, but by my attempt no json object is passed anymore. Can anyone help me with this? I would be very grateful for any advice!
Reduce array of objects by user_id and sum certain values
I have an array of objects that looks like this: "data": [ { "workout_id": 1, "user_id": 1, "shotLocation": 27, "shotAttempts": 20,"shotsMade": 19, "id": 1 }, { "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 2 }, { "workout_id": 2, "user_id": 1, "shotLocation": 10, "shotAttempts": 10, "shotsMade": 7, "id": 3 }, { "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 29, "id": 4 }, { "workout_id": 3, "user_id": 5, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 5 }, { "workout_id": 4, "user_id": 6, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 15, "id": 6 }, { "workout_id": 4, "user_id": 6, "shotLocation": 2, "shotAttempts": 20, "shotsMade": 14, "id": 7 } ] I would like to create a new array of objects where I reduce the user_ids to each unique user then calculate the total shotAttempts and shotsMade for each user like this: "dataTotals" : [ {"id": 1, "user_id": 1, "shotAttempts": 90, "shotsMade": 64} {"id": 2, "user_id": 5, "shotAttempts": 10, "shotsMade": 9} {"id": 3, "user_id": 6, "shotAttempts": 50, "shotsMade": 29} I plan on iterating over this new array of objects with a .map function to populate a leaderboard as more users record their data. I've tried several different ways to achieve this based on what I've read about the .map and .reduce methods, as well as searching here and found something that's close, but it returns an object as indicated below. like this: // create new object to store results in let newObj = {}; // loop through user objects this.state.shotlogs.forEach(function(shotlog){ // check if user_id has already been added to newObj if(!newObj[shotlog.user_id]){ // If it is the first time seeing this user_id // we need to add shots attempted and shots made to prevent errors newObj[shotlog.user_id] = {}; newObj[shotlog.user_id]['user_id'] = shotlog.user_id; newObj[shotlog.user_id]['shotAttempts'] = 0; newObj[shotlog.user_id]['shotsMade'] = 0; } // add shots attempted and made to newObj for this user newObj[shotlog.user_id]['shotAttempts'] += shotlog.shotAttempts newObj[shotlog.user_id]['shotsMade'] += shotlog.shotsMade }) but this returns an object that looks like this and not an array of objects like above: { 1: {user_id: 1, shotAttempts: 70, shotsMade: 64} 5: {user_id: 5, shotAttempts: 10, shotsMade: 9} 6: {user_id: 6, shotAttempts: 50, shotsMade: 29} } Any help for this new coder is greatly appreciated!!
const data = [ { workout_id: 1, user_id: 1, shotLocation: 27, shotAttempts: 20, shotsMade: 19, id: 1 }, { workout_id: 2, user_id: 1, shotLocation: 1, shotAttempts: 10, shotsMade: 9, id: 2 }, { workout_id: 2, user_id: 1, shotLocation: 10, shotAttempts: 10, shotsMade: 7, id: 3 }, { workout_id: 2, user_id: 1, shotLocation: 1, shotAttempts: 30, shotsMade: 29, id: 4 }, { workout_id: 3, user_id: 5, shotLocation: 1, shotAttempts: 10, shotsMade: 9, id: 5 }, { workout_id: 4, user_id: 6, shotLocation: 1, shotAttempts: 30, shotsMade: 15, id: 6 }, { workout_id: 4, user_id: 6, shotLocation: 2, shotAttempts: 20, shotsMade: 14, id: 7 } ]; const shooters = data.reduce( (results, current) => ({ ...results, [current.user_id]: { user_id: current.user_id, shotAttempts: current.shotAttempts + (results[current.user_id] ? results[current.user_id].shotAttempts : 0), shotsMade: current.shotsMade + (results[current.user_id] ? results[current.user_id].shotsMade : 0) } }), {} ); console.log(shooters);
You can use Array.reduce, Object.values with ES6 destructuring and solve this in a concise manner: const data = [{ "workout_id": 1, "user_id": 1, "shotLocation": 27, "shotAttempts": 20, "shotsMade": 19, "id": 1 }, { "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 2 }, { "workout_id": 2, "user_id": 1, "shotLocation": 10, "shotAttempts": 10, "shotsMade": 7, "id": 3 }, { "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 29, "id": 4 }, { "workout_id": 3, "user_id": 5, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 5 }, { "workout_id": 4, "user_id": 6, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 15, "id": 6 }, { "workout_id": 4, "user_id": 6, "shotLocation": 2, "shotAttempts": 20, "shotsMade": 14, "id": 7 } ] const result = data.reduce((r,{workout_id, user_id, shotAttempts, shotsMade}) => { r[user_id] = r[user_id] || {id: workout_id, user_id, shotAttempts: 0, shotsMade: 0} r[user_id].shotAttempts += shotAttempts r[user_id].shotsMade += shotsMade return r }, {}) console.log(Object.values(result))
You can use Array.prototype.reduce() Code: const data = [{ "workout_id": 1, "user_id": 1, "shotLocation": 27, "shotAttempts": 20,"shotsMade": 19, "id": 1 },{ "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 2 },{ "workout_id": 2, "user_id": 1, "shotLocation": 10, "shotAttempts": 10, "shotsMade": 7, "id": 3 },{ "workout_id": 2, "user_id": 1, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 29, "id": 4 },{ "workout_id": 3, "user_id": 5, "shotLocation": 1, "shotAttempts": 10, "shotsMade": 9, "id": 5 },{ "workout_id": 4, "user_id": 6, "shotLocation": 1, "shotAttempts": 30, "shotsMade": 15, "id": 6 },{ "workout_id": 4, "user_id": 6, "shotLocation": 2, "shotAttempts": 20, "shotsMade": 14, "id": 7 }]; const dataTotals = Object.values(data.reduce((a, c) => { if (!a[c.user_id]) { a[c.user_id] = { id: c.workout_id, user_id: c.user_id, shotAttempts: c.shotAttempts, shotsMade: c.shotsMade }; } else { a[c.user_id].shotAttempts += c.shotAttempts; a[c.user_id].shotsMade += c.shotsMade; } return a; }, {})); console.log(dataTotals);
how to find difference between two array using lodash/underscore in nodejs
I have two arrays of arrays and am trying to find the difference. var a = [[ 11, 24, 28, 38, 42, 44 ], [ 7, 19, 21, 22, 29, 38 ], [ 2, 21, 27, 30, 33, 40 ], [ 6, 11, 12, 21, 34, 48 ], [ 1, 10, 17, 31, 35, 40 ], [ 1, 18, 26, 33, 36, 45 ], [ 15, 21, 22, 24, 38, 46 ], [ 5, 17, 21, 27, 29, 41 ], [ 3, 7, 12, 16, 20, 28 ], [ 9, 12, 13, 18, 30, 37 ], [ 3, 19, 21, 31, 33, 46 ], [ 6, 11, 16, 18, 20, 34 ], [ 1, 3, 11, 13, 24, 28 ], [ 12, 13, 16, 40, 42, 46 ], [ 1, 3, 5, 36, 37, 41 ], [ 14, 15, 23, 24, 26, 31 ], [ 7, 13, 14, 15, 27, 28 ]]; var b = [[ 4, 7, 9, 21, 31, 36 ], [ 2, 5, 6, 12, 15, 21 ], [ 4, 7, 8, 15, 38, 41 ], [ 11, 24, 28, 38, 42, 44 ], [ 7, 19, 21, 22, 29, 38 ]]; How would I find: c = [[ 2, 21, 27, 30, 33, 40 ], [ 6, 11, 12, 21, 34, 48 ], [ 1, 10, 17, 31, 35, 40 ], [ 1, 18, 26, 33, 36, 45 ], [ 15, 21, 22, 24, 38, 46 ], [ 5, 17, 21, 27, 29, 41 ], [ 3, 7, 12, 16, 20, 28 ], [ 9, 12, 13, 18, 30, 37 ], [ 3, 19, 21, 31, 33, 46 ], [ 6, 11, 16, 18, 20, 34 ], [ 1, 3, 11, 13, 24, 28 ], [ 12, 13, 16, 40, 42, 46 ], [ 1, 3, 5, 36, 37, 41 ], [ 14, 15, 23, 24, 26, 31 ], [ 7, 13, 14, 15, 27, 28 ]]; I had tried underscore: _ = require('underscore'); _.difference(a,b); But it doesn't work. I also tried lodash: _ = require('lodash'); _.differenceBy(a,b); but it doesn't work either. What am I doing wrong here?
Use _.differenceWith, and pass a comparator which compares two arrays, as in: _.differenceWith(a, b, _.isEqual);
As mentioned by #dsl101, _.xor([1, 2, 3], [2, 3, 4]); // [1, 4]
Add values to an object from another array of objects
So i have this scope with a predefined equipment: $scope.equipment = [ {slot: 7, colspan: 2, rowspan: 1}, {slot: 0, colspan: 2, rowspan: 2}, {slot: 10, colspan: 2, rowspan: 1}, {slot: 8, colspan: 1, rowspan: 1}, {slot: 9, colspan: 1, rowspan: 1}, {slot: 6, colspan: 2, rowspan: 1}, {slot: 17, colspan: 1, rowspan: 1}, {slot: 18, colspan: 1, rowspan: 1}, {slot: 1, colspan: 2, rowspan: 4}, {slot: 19, colspan: 2, rowspan: 1}, {slot: 4, colspan: 2, rowspan: 4}, {slot: 5, colspan: 2, rowspan: 4}, {slot: 14, colspan: 1, rowspan: 1}, {slot: 15, colspan: 1, rowspan: 1}, {slot: 2, colspan: 2, rowspan: 2}, {slot: 16, colspan: 1, rowspan: 1}, {slot: 15, colspan: 1, rowspan: 1}, {slot: 3, colspan: 2, rowspan: 2} ]; As you can see, each slot has its own colspan and rowspan. Than i have the equipment items array of objects: $scope.equipmentItems = [ { "id": 282, "upgrade": 15, "bind": 1, "slot": 0, "name": "Archridium Headpiece (BL)" }, { "id": 147, "upgrade": 15, "bind": 1, "slot": 1, "name": "Archridium Suit (BL)" }, { "id": 192, "upgrade": 15, "bind": 1, "slot": 2, "name": "Archridium Hands (BL)" }, { "id": 237, "upgrade": 15, "bind": 1, "slot": 3, "name": "Archridium Shoes (BL)" }, { "id": 3706, "upgrade": 0, "bind": 0, "slot": 4 }, { "id": 3707, "upgrade": 0, "bind": 0, "slot": 5 }, { "id": 3622, "upgrade": 0, "bind": 0, "slot": 6 }, { "id": 408, "upgrade": 0, "bind": 0, "slot": 7, "name": "Amulet Of Pain +7", "description": "Protect you from enemy's attacks and to give more chance to do Critical Attacks. " }, { "id": 3194, "upgrade": 0, "bind": 0, "slot": 8, "name": "Ring of Luck +3", "description": "Increases your Critical Hit Rate." }, { "id": 3193, "upgrade": 0, "bind": 0, "slot": 9, "name": "Critical Ring +3", "description": "Increases your Critical Attack Damage." }, { "id": 2371, "upgrade": 0, "bind": 1, "slot": 10, "name": "Astral Board Card - K Red Crystal Edition", "description": "A mysterious card that summons Astral Board, one of the valuable Honorable Age's legacies and the essence of Core Technology. - K Red Crystal Edition_$4#(Right click to use) " }, { "id": 3607, "upgrade": 0, "bind": 0, "slot": 13 }, { "id": 3607, "upgrade": 0, "bind": 0, "slot": 14 }, : { "id": 3604, "upgrade": 0, "bind": 0, "slot": 15 }, { "id": 3604, "upgrade": 0, "bind": 0, "slot": 16 }, { "id": 2568, "upgrade": 0, "bind": 4, "slot": 17, "name": "Leth Tyrant's Ring", "description": "This ring contains the sealed power of Leth Tyrant, the extraordinary monster created by the Doctor. " }, { "id": 3184, "upgrade": 0, "bind": 4, "slot": 18, "name": "Killian's Ring", "description": "This generates powerful energy from the combination of Killian's Dark Energy and the grudge of the Black Cat, the Guardian of Hell." }, "{ "id": 2228, "upgrade": 0, "bind": 0, "slot": 19, "name": "Belt of Damp +4", "description": "This belt reduces damage from intensified sword or magic attacks. " } ]; As you can see each object key is basically the slot id. Than i use this code $scope.equipmentItems = _.sortBy($scope.equipmentItems, function(obj) { return _.indexOf([7, 0, 10, 8, 9, 6, 17, 18, 1, 19, 4, 5, 14, 13, 2, 16, 15, 3], obj.slot); }); to sort the equipment items keys to my specific order. Anyway, what i want is the most performant piece of code that can add the values colspan and rowspan from $scope.equipment to $scope.equipmentItems WHERE $scope.equipment slot correspond to $scope.equipmentItems slot.
You can use Array functions. Something like this: let merged = equipmentItems.map(item=>{ item.anotherKey = equipment.find(it=> it.slot == item.slot); return item; }); If you don't like this, or you can't use those methods you can use forEach equipmentItems.forEach(function(item){ equipment.forEach(function(item_){ if(item.slot == item_.slot){ item.someKey = item_; } }); });
Summarize array values, underscore
Got this array: var arr = [ { series: [ { "name": 2014, "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30] }, ] }, { series: [ { "name": 2014, "data": [32, 17, 15, 12, 33 10, 33, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [45, 10, 12, 55, 77, 7, 6, 8, 8, 11, 33, 30] }, ] }, ] I need to create a function that returns a summrized series: var series = [ { year: '2014', data: [51, 34....], }, { year: '2015', data: [63, 27....], } ] I could to loops to do this but I guess there is some smart way of doing with underscore? Probably with the reduce function. Any ideas of how to do this?
var arr = [{ "series": [{ "name": 2014, "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30] }] }, { "series": [{ "name": 2014, "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30] }] }]; _.chain(arr) .map('series') .flatten() .reduce(function(object, value) { object[value.name] = !object[value.name] ? value.data : _.union(object[value.data], value.data); return object; }, {}) .reduce(function(arr, value, key) { arr.push({ year: key, data: value }); return arr; }, []) .value();
You could use some iterations and an object as reference to the year for grouping. var arr = [{ series: [{ "name": 2014, "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30] }] }, { series: [{ "name": 2014, "data": [32, 17, 15, 12, 33, 10, 33, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [45, 10, 12, 55, 77, 7, 6, 8, 8, 11, 33, 30] }] }], series = []; arr.forEach(function (a) { Object.keys(a).forEach(function (k) { a[k].forEach(function (b) { if (!this[b.name]) { this[b.name] = { year: b.name, data: [] }; series.push(this[b.name]); } b.data.forEach(function (c, i) { this[b.name].data[i] = (this[b.name].data[i] || 0) + c; }, this); }, this); }, this); }, {}); document.write('<pre>' + JSON.stringify(series, 0, 4) + '</pre>');