underscore.js

Count total with two criterias using Lodash

主宰稳场 提交于 2019-12-11 07:00:01
问题 I can't figure out how I can count the total devices based on location and unique serial number. { "dates": [ { "date": "Sep 1, 2014", "devices": [ { "model": "Canon", "location": "Chicago", "serialNum": "abc123" }, { "model": "Canon", "location": "Chicago", "serialNum": "xyz456" }, { "model": "HP", "location": "New York", "serialNum": "123456" }, { "model": "Brother", "location": "Chicago", "serialNum": "DEF777" } ] }, { "date": "Sep 2, 2014", "devices": [ { "model": "Canon", "location":

How to create own inheritable class with underscore

守給你的承諾、 提交于 2019-12-11 06:37:58
问题 I want to built a Backbone.Model like object/class. I looked through the Backbone docs and found that they create a inheritable object/class with its inheritable attributes, methods in two steps: I. Create a function with some attributes var Model = Backbone.Model = function(attributes, options) { var defaults; attributes || (attributes = {}); //? if (options && options.parse) attributes = this.parse(attributes); //? if (defaults = getValue(this, 'defaults')) { attributes = _.extend({},

How to execute functions with delay, underscorejs/lodash

旧巷老猫 提交于 2019-12-11 06:19:27
问题 Background I have a JavaScript function that prints numbers to the screen. This function prints a lot of numbers, but I am using a screen as old as Alan Turing so it can't print the numbers so fast! The solution? Throtlle/debounce/delay the prints using underscorejs or lodash. Tentative My first tentative was to use throttle . let fun = num => { console.log(num); }; let throttleFun = _.throttle(fun, 1000); for (let i = 0; i < 10; i++) { throttleFun(i); } <script src="https://cdnjs.cloudflare

Sorting Json data based on a field

梦想与她 提交于 2019-12-11 05:39:36
问题 I have a Json data that I need to sort before display it. My Json is as below. I need to sort them based on the ColumnLocation. [{ "Name": "PieChart", "Id": "1", "ColumnLocation": "0", "RowLocation": "0" }, { "Name": "Calendar", "Id": "2", "ColumnLocation": "1", "RowLocation": "0" }, { "Name": "FavouriteFilter", "Id": "3", "ColumnLocation": "2", "RowLocation": "0" }, { "Name": "FilterResults", "Id": "4", "ColumnLocation": "0", "RowLocation": "1" }, { "Name": "Watched", "Id": "5",

CSS Hover effect only on click

▼魔方 西西 提交于 2019-12-11 05:38:13
问题 I have a group of boxes with a hover effect and I want the individual boxes to be flipped only when clicked. I am trying to remove this block of css: /*.flip-container:hover .flipper { transform: rotateY(180deg); }*/ And use underscorejs to add it back only when clicked but not working: window.onload = function(){ var memoryCards = document.getElementsByClassName("flipper"); _.each(memoryCards, function (card){ this.addEventListener("click", function(){ this.setAttribute("transform", "rotateY

Recreate the Json by identifying the missing values

人盡茶涼 提交于 2019-12-11 05:37:22
问题 I have a Json data that I need adjust before sending it to my component. My Json is as below. I need to identify the missing fields and move the below ones up. [{ "Id": "a", "ColumnLocation": "0", "RowLocation": "0" }, { "Id": "b", "ColumnLocation": "0", "RowLocation": "1" }, { "Id": "4", "ColumnLocation": "0", "RowLocation": "3" }, { "Id": "c", "ColumnLocation": "1", "RowLocation": "0" }, { "Id": "d", "ColumnLocation": "1", "RowLocation": "2" }, { "Id": "e", "ColumnLocation": "2",

Sort JavaScript Array in Artificial Order

▼魔方 西西 提交于 2019-12-11 04:07:15
问题 I have an array returned by a REST query that represents the number of items in a multi-step production process. var steps = [ { name:'Package', value:3}, { name:'Assemble', value:1 }, { name:'Ship', value:7}, { name:'Preprocess', value:9 }, { name:'Paint', value:5 } ]; I'd like to sort them in the order of the process, like this: Preprocess Paint Assemble Package Ship I have other alphanumeric sorts that I am doing with Underscore but I cannot figure this one out. 回答1: You could take an

Sort nested array by value?

落爺英雄遲暮 提交于 2019-12-11 03:45:31
问题 I have a nested array like the following: data = [ [Date.UTC(2013, 1, 1), 1], [Date.UTC(2013, 1, 5), 22], [Date.UTC(2013, 1, 2), 2], [Date.UTC(2013, 1, 11), 33] ] I am using underscore and I am trying to figure out a way to sort it by the Date.UTC so the array shows dates by first to last or lowest to highest ? 回答1: You could use sortBy with a function to pick off the first elements of the inner arrays: sortBy _.sortBy(list, iterator, [context]) Returns a sorted copy of list , ranked in

Check if duplicate array pairs exist using underscore only

怎甘沉沦 提交于 2019-12-11 03:22:56
问题 I am wondering how I can check if a duplicate pair of values in an array exist as part of a larger array in javascript. You can see there is a duplicate pair of [1,2] - so the function should just return true . i.e var arr = [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14], [1,2]] I have tried using this logic which gives me a clean array and a "true" var unique = []; var done = []; var dup = false; for(var x = 0; x < arr.length; x++) { var myStr = arr[x].toString(); if(done.indexOf

consolidating values in a list of objects based on an attribute which is the same

做~自己de王妃 提交于 2019-12-11 02:49:18
问题 Bit of background, this comes from a submitted form that I used serializeArray() on I have a list of objects like so. [ {name: 0, value: 'waffles'}, {name: 0, value: 'pancakes'}, {name: 0, value: 'french toast'}, {name: 1, value: 'pancakes'} ] I want to take all things that have the same name attribute and put them together. EG, [ {name: 0, value: ['waffles', 'pancakes', 'french toast']}, {name: 1, value: ['pancakes']} ] how would one go about this? All the things I've tried only result in