underscore.js

How can I determine which array index my number is closest to? [duplicate]

血红的双手。 提交于 2019-12-11 22:37:16
问题 This question already has answers here : get closest number out of array (17 answers) Closed 5 years ago . I have an array and var that look like this: var paymentMilestones = [0, 30, 60, 90]; var daysOverdue = 33; In JavaScript, how can I programmatically determine which of the paymentMilestones indexes the daysOverdue var is closest to? I need that value returned, so in this case the function should return 30 . 回答1: A quick answer (if there is a tie it will bring back both - assuming the

2d array to associative array using underscore's _.map

给你一囗甜甜゛ 提交于 2019-12-11 20:32:44
问题 I'm trying to map 2d array: var fkOptionList2d = [[3, 'Orange'],[5, 'Banana'],[6, 'Coconut']] to an associative array: var fkOptionList1d = [{id: 1, label: 'Orange'},{id: 2, label: 'Banana'},{id: 3, label: 'Coconut'}] but I'm new to underscore.js and don't quite get it yet. Should it be something like: fkTableArr1d = _.object(_.map(fkTableArr2d, function(item, id) { return [{"id: " + id,"label: " + item}] })); ? 回答1: As per my understanding you need to use return [{ "id " : id, "label " :

Backbone collection View add not being called with a model

心已入冬 提交于 2019-12-11 19:49:54
问题 I've my models var Client = Backbone.Model.extend({}); var Colony = Backbone.Collection.extend({ url: '/presence/knock', model: Client }); views var ClientView = Backbone.View.extend({ initialize: function(){ this.render(); }, template: _.template('...'), render: function(){ this.$el.html(this.template(this.model.toJSON())); return this; } }); var ColonyView = Backbone.View.extend({ initialize: function(){ _.bindAll(this, 'addOne', 'addAll'); this.collection.bind('add', this.addOne); this

filter multi-dimension JSON arrays

爷,独闯天下 提交于 2019-12-11 19:38:32
问题 Here is the JSON details: var array={ "app": { "categories": { "cat_222": { "id": "555", "deals": [{ "id": "73", "shop": "JeansWest" }, { "id": "8630", "shop": "Adidas" }, { "id": "11912", "shop": "Adidas" }] }, "cat_342": { "id": "232", "deals": [{ "id": "5698", "shop": "KFC" }, { "id": "5701", "shop": "KFC" }, { "id": "5699", "shop": "MC" }] } } } } I've tried to filter the array to have shop which contain da pattern. var filted = _.filter(array.app.categories,function(item) { return _.any

underscore _,template not recognize variables on template

旧巷老猫 提交于 2019-12-11 19:29:28
问题 I´m using underscore to load a html template with by require.js with text.js, like code bellow: template: _.template(listItemTemplate) , render: function () { $(this.el).html(this.template(this.model.toJSON)); return this; } tvListItemTemplate.html <h4><%= _id%></h4> If i do console.log(this.model.toJSON()) it prints the following: But the console give me this error: I don´t understand why 回答1: Check with: $(this.el).html(_.template(listItemTemplate, this.model)); or template: function(x) { _

Remove square brackets at beginning and ending using Undersore or Javascript

拥有回忆 提交于 2019-12-11 19:02:02
问题 I trying to remove array square bracket at beginning and end. EX: [['moe', 30], ['larry', 40], ['curly', 50]] Need: ['moe', 30], ['larry', 40], ['curly', 50] 回答1: If it is a string you can use the substring or slice method like this: var yourString = "[['moe', 30], ['larry', 40], ['curly', 50]]"; // using substring var result = yourString.substring(1, yourString.length-1); console.log(result); // using slice var result2 = yourString.slice(1, -1); console.log(result2); 来源: https:/

Use attributes with weird chars in Marionette / underscore template

♀尐吖头ヾ 提交于 2019-12-11 18:23:32
问题 I have a model with attributes names like @id , @type , etc. If I try to use <%= @id %> in a Marionette.ItemView template (with Underscore) I get Uncaught SyntaxError: Unexpected token ILLEGAL Using the syntax ['@id'] does not produce the expected result. Do I have to override the serializeData function? Thanks 回答1: Underscore templates require JavaScript expressions inside <%= ... %> , the compiled template uses with so you can usually reference object properties as though they were

Backbone collection is _.groupedBy but not render doesn't work

走远了吗. 提交于 2019-12-11 18:04:40
问题 I am struggling with my _.groupBy function. With this code I have rendered in the browser something like this: align spellcheck isContentEditable contentEditable Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 outerText outerHTML And here is my whole code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Backbone test</title> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css" rel="stylesheet"> <meta name="viewport" content="width=device

Using underscore.js in chrome console

柔情痞子 提交于 2019-12-11 17:32:19
问题 I am learning JavaScript and came to a point wherein I have to load underscore.js in the chrome console. I did some search and ended at this stackflow response: How to enable to use underscore in console in Chrome developer tool? The code snippet provided in the above link works for me. But I could not figure out what does window.R object refer to? Why do I need to include this? Can't I just add the script element and load the underscorejs? I am referring the below piece of code in the

How to call a function with params in Iframe after loading the iframe in the pop-up dynamically created?

爱⌒轻易说出口 提交于 2019-12-11 16:37:06
问题 In my app, I am loading search result. while the user click on the print button I would like to create a pop-up . ( this is ok ) But when I create a pop-up , I would like to load my Iframe in the pop-up . Again I need to call a Iframe function with the parameter which i receiving from parent page, Because I am using underscore template. I would like to call couple of ajax request with api url - (which i getting from parent page as parameter ) and need to create and append the html in to the