underscore.js

Turning an array into an array of objects with Underscore.js?

限于喜欢 提交于 2019-12-25 12:44:14
问题 I have an array: var countries = ['Austria', 'America', 'Australia']; I know you can turn that into an object with Underscore.js like this: _.object(['name', 'name2', 'name3'], countries)); How can I turn the array into an array of objects that looks like this? var countriesObject = [ { name: 'Austria' }, { name: 'America' }, { name: 'Australia' } ]; (with all the keys named name ). 回答1: No need to use Underscore.js for that. You can do it with plain javascript: var new_arr = []; countries

How to break up an object with a string of values to create a new array of objects?

时光总嘲笑我的痴心妄想 提交于 2019-12-25 12:44:12
问题 I was working on a solution to another question posed, and I came up with a solution, but I'm convinced that there's a more elegant way to do it. Let's say that you have an object where all of the values are a string of values separate by commas, like this: { "action" : "goto,goto", "target" : "http://www.google.com,http://www.cnn.com" } But, you'd like to separate the values and break up the object into an array of objects, like this: [ { "action" : "goto", "target" : "http://www.google.com"

Unique array of objects and merge duplications using lodash or underscore

吃可爱长大的小学妹 提交于 2019-12-25 09:31:27
问题 I have an array of objects that can contain duplicates, but I'd like uniquify the array by a property, and then I want to merge properties of the duplicates conditionally. For example, give the array below var array = [ { name: 'object1', propertyA: true, propertyB: false, propertyC: false }, { name: 'object2', propertyA: false, propertyB: true, propertyC: false }, { name: 'object1', propertyA: false, propertyB: true, propertyC: false } ] Knowing each object has name, propertyA, propertyB,

Merging two json array object based on union and intersection

馋奶兔 提交于 2019-12-25 09:12:01
问题 I am trying to merge two json array with objects as element. You may refer to this plunkr file for both json. I have succesfully retrieve the expected final outcome array id, but I do not know how to form back the expected json as below. I am using underscore js for this purpose. Note: If object exist in newJson and not in currentJson, after merge, it will be inactive state by default. I am not sure whether I am using the correct approach. This is what I have try: var newJsonID = _.pluck

Underscore JS difference on array and array with objects

纵然是瞬间 提交于 2019-12-25 07:14:06
问题 I'm having an array with certain numbers and an array with certain objects, looking like this: var names = [ { id: 1, name: 'Alex'}, { id: 2, name: 'John'}, { id: 3, name: 'Mary'} ]; var blocked_ids = [1, 2]; Now I would like to remove the objects with the blocked_ids from the names array. So the result would be this: [ { id: 3, name: 'Mary'} ] As you can see the objects with id 1 and 2 are gone, because the array "blocked_ids" contained these numbers. If it where just two arrays, i could use

sortby using Underscore

痴心易碎 提交于 2019-12-25 07:03:55
问题 I am trying to sort certain records using underscore _.sortby() function . My requirement is to give higher priority for names starting with _ . For eg . if my code is var patients = [ [{name: 'John', roomNumber: 1, bedNumber: 1}], [{name: 'Lisa', roomNumber: 1, bedNumber: 2}], [{name: '_A', roomNumber: 2, bedNumber: 1}], [{name: 'Omar', roomNumber: 3, bedNumber: 1}] ]; var sortedArray = _.sortBy(patients, function(patient) { return patient[0].name; }); I would want to have the record _A,

JavaScript extend array

帅比萌擦擦* 提交于 2019-12-25 05:15:14
问题 I have an array of objects. During the loop I append different properties to each entry. My question - how do I make sure every entry has all the properties of each entry? Let's consider this: var myArray = [{A: 1}, {B: 2}, {C: 3}]; Now I want to run some elegant one-liner to convert this array into: [{A: 1, B:2, C: 3}, {A: 1, B:2, C: 3}, {A: 1, B:2, C: 3}] 回答1: An elegant one-liner is a bit difficult without a library. But if you have some kind of extend function, then it could be: myArray

Underscore, Nested Group By and Generate a JSON

与世无争的帅哥 提交于 2019-12-25 04:19:58
问题 I have an array of objects with duplicates and I'm trying to get a unique listing, where uniqueness is defined by a subset of the properties of the object. For example, Current JSON Object: [{"x":6811,"y":15551,"a":"a"}, {"x":6811,"y":15551,"a":"b"}, {"x":6811,"y":15551,"a":"c"}, {"x":6811,"y":15552,"a":"c"}, {"x":6812,"y":15551,"a":"c"}] How to group by two property The last result is [{"x":6811,"y":15551,"a":["a","b","c"]}, {"x":6811,"y":15552,"a":["c"]}, {"x":6812,"y":15551,"a":["c"]}] How

events called from inside another function jQuery with underscore template doesn't work

狂风中的少年 提交于 2019-12-25 03:58:27
问题 I have a function which i used to add elements to a list, I want to have events run on interaction with this list, e.g. click. If I do it using the document object it works well however if I use jQuery with underscore templates the element is successfully appended but the events will not trigger. var addElement = function(parentElement){ //would work this.thisElement = document.createElement('li'); parentElement.appendChild(thisElement); $(this.thisElement).click(function(event){ alert(

Typeerror: n is undefined in underscore.min.js

倖福魔咒の 提交于 2019-12-25 03:54:52
问题 The following code: var template = _.template( $("#search_template").html() ); throws this error in the compiler when I try to run it: Typeerror: n is undefined in underscore.min.js 5:14721 Does anyone have an idea what might be going wrong? Theoretically it should be valid code. I have also tried using the following code instead: var template = _.template( $("#search_template").html(), variables ); Which results in exactly the same error. When I try to run with underscore.js instead get the