lodash

Sort array of objects with date field by date

为君一笑 提交于 2019-12-03 05:44:04
问题 Give the following array of objects, I need to sort them by the date field ascending. var myArray = [ { name: "Joe Blow", date: "Mon Oct 31 2016 00:00:00 GMT-0700 (PDT)" }, { name: "Sam Snead", date: "Sun Oct 30 2016 00:00:00 GMT-0700 (PDT)" }, { name: "John Smith", date: "Sat Oct 29 2016 00:00:00 GMT-0700 (PDT)" } ]; so that in this example, the final result would be John Smith, Sam Snead, and Joe Blow. I am trying to use lodash's _.sortBy(), but I can't get any sorting to take place no

return object property using lodash from array

早过忘川 提交于 2019-12-03 04:56:06
问题 I have been trying to return a property of an object by filtering it first. Here's what I did: var characters = [ { 'name': 'barney', 'age': 36, 'blocked': false }, { 'name': 'fred', 'age': 40, 'blocked': true }, { 'name': 'pebbles', 'age': 1, 'blocked': false } ]; _.find(characters, function(chr) { return chr.age == 40 }); It returns whole object where as I want to return specific property. Can anyone guide me how can I do it? Any help will be appreciated. 回答1: You could use the Lodash

How can I use lodash/underscore to sort by multiple nested fields?

大兔子大兔子 提交于 2019-12-03 04:23:07
I want to do something like this: var data = [ { sortData: {a: 'a', b: 2} }, { sortData: {a: 'a', b: 1} }, { sortData: {a: 'b', b: 5} }, { sortData: {a: 'a', b: 3} } ]; data = _.sortBy(data, ["sortData.a", "sortData.b"]); _.map(data, function(element) {console.log(element.sortData.a + " " + element.sortData.b);}); And have it output this: "a 1" "a 2" "a 3" "b 5" Unfortunately, this doesn't work and the array remains sorted in its original form. This would work if the fields weren't nested inside the sortData . How can I use lodash/underscore to sort an array of objects by more than one nested

How to repeat an element n times using JSX

浪子不回头ぞ 提交于 2019-12-03 04:04:26
问题 I am using React/JSX in my app in order to accomplish what I want, also, Lodash. I need to repeat an element a certain amount of times depending on a condition, how should I do that? Here is the element: <span className="busterCards">♦</span>; And I am assigning it like this: let card; if (data.hand === '8 or more cards') { card = <span className="busterCards">♦</span>; } So in this case, I need to repeat the element 8 times. What should be the process using Lodash? 回答1: Here you go: let card

Why is my webpack bundle.js and vendor.bundle.js so incredibly big?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: All my React projects tend to be incredibly large in file size (bundle.js is 4.87 mb and the vendor.bundle.js is 2,87 mb). I have no idea why it is this large. I already have uglifyJS on, but this doesn't seem to help a lot (5.09 > 4.87mb and 2.9 > 2.87mb) var webpack = require('webpack'); var ExtractTextPlugin = require("extract-text-webpack-plugin"); require('es6-promise').polyfill(); var config = { entry: { app: [ './src/entry.jsx' ], vendor: [ 'react', 'lodash', 'superagent' ] }, output: { path: './build', filename: "bundle.js" }, eslint

Unknown provider CookieStore

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am having service where I want to use the $cookieStore module. It works fine, but when unit testing it breaks, and gives the error: "$cookieStoreProvider <- $cookieStore <- filtersService". The service looks like this: serviceModule.factory('filtersService', ['$rootScope', '$location', '$cookieStore', function($rootScope, $location, $cookieStore){ return { getFilters: function(){...} } And the unit test service looks like this: describe('filtersService tests', function(){ var filtersService; beforeEach(module('App.services')); beforeEach

Comparing two arrays of objects, and exclude the elements who match values into new array in JS

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: here is my use case in JavaScript: I have two arrays of objects which have properties that match (id & name). var result1 = [ {id:1, name:'Sandra', type:'user', username:'sandra'}, {id:2, name:'John', type:'admin', username:'johnny2'}, {id:3, name:'Peter', type:'user', username:'pete'}, {id:4, name:'Bobby', type:'user', username:'be_bob'} ]; var result2 = [ {id:2, name:'John', email:'johnny@example.com'}, {id:4, name:'Bobby', email:'bobby@example.com'} ]; var props = ['id', 'name']; My goal is to have another array of objects containing only

using lodash .groupBy. how to add your own keys for grouped output?

匿名 (未验证) 提交于 2019-12-03 02:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have this sample data returned from an API. I'm using Lodash's _.groupBy to convert the data into an object I can use better. The raw data returned is this: [ { "name" : "jim" , "color" : "blue" , "age" : "22" }, { "name" : "Sam" , "color" : "blue" , "age" : "33" }, { "name" : "eddie" , "color" : "green" , "age" : "77" } ] I want the _.groupBy function to return an object that looks like this: [ { color : "blue" , users : [ { "name" : "jim" , "color" : "blue" , "age" : "22" }, { "name" : "Sam" , "color" : "blue" , "age" : "33" }

How to consume npm modules from typescript?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm giving a shot at typescript. It works fine at the hello world stage. I'm now trying to use a npm module : index.ts = import _ = require('lodash') console.log(_.toUpper('Hello, world !')) This doesn't work : tsc index.ts -> Cannot find module 'lodash'. (2307) node-ts index.js -> Cannot find module 'lodash'. (2307) Looking at typescript documentation and in google didn't help. Other S/O questions are either unanswered ( here and here ) or unrelated. Elements : typescript 1.8 latest Yes, lodash is installed npm i --save lodash and exists in

lodash - project/transform object into key value array

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm about to use forOwn to iterate through an object's properties and create an array manually and can't helping thinking there's a oneliner already available to do it. { prop1 : "value" , prop2 : { sub : 1 } } to: [ { key : "prop1" , value : "value" }, { key : "prop2" , value : { sub : 1 }} ] Thanks 回答1: You can use lodash's _.map() : var obj = { prop1 : "value" , prop2 : { sub : 1 } }; var result = _ . map ( obj , function ( value , prop ) { return { prop : prop , value : value }; }); console . log ( result ); Or you can do it