lodash

lodash: mapping array to object

Deadly 提交于 2019-11-30 11:30:29
问题 Is there a built-in lodash function to take this: var params = [ { name: 'foo', input: 'bar' }, { name: 'baz', input: 'zle' } ]; And output this: var output = { foo: 'bar', baz: 'zle' }; Right now I'm just using Array.prototype.reduce() : function toHash(array, keyName, valueName) { return array.reduce(function(dictionary, next) { dictionary[next[keyName]] = next[valueName]; return dictionary; }, {}); } toHash(params, 'name', 'input'); Wondering if there's a lodash short-cut. 回答1: Another way

Replacing objects in array

佐手、 提交于 2019-11-30 11:20:53
问题 I have this javascript object: var arr1 = [{id:'124',name:'qqq'}, {id:'589',name:'www'}, {id:'45',name:'eee'}, {id:'567',name:'rrr'}] var arr2 = [{id:'124',name:'ttt'}, {id:'45',name:'yyy'}] I need to replace objects in arr1 with items from arr2 with same id . So here is the result I want to get: var arr1 = [{id:'124',name:'ttt'}, {id:'589',name:'www'}, {id:'45',name:'yyy'}, {id:'567',name:'rrr'}] How can I implement it using javascript? 回答1: You can use Array#map with Array#find. arr1.map

use Lodash to sort array of object by value

ε祈祈猫儿з 提交于 2019-11-30 10:24:18
问题 I am trying to sort an array by 'name' value (using Lodash). I used the Lodash docs to create the solution below however .orderBy doesn't seem to be having any affect at all. Can anyone shed some light on the correct way to sort array? Chars Array [ { "id":25, "name":"Anakin Skywalker", "createdAt":"2017-04-12T12:48:55.000Z", "updatedAt":"2017-04-12T12:48:55.000Z" }, { "id":1, "name":"Luke Skywalker", "createdAt":"2017-04-12T11:25:03.000Z", "updatedAt":"2017-04-12T11:25:03.000Z" } ] Function

How to compare two array of object and get common objects

不羁岁月 提交于 2019-11-30 09:54:04
问题 Hello guys I have two arrays var elements = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "type": "label", "id": "id_234" }, { "id": "id_16677", "type": "div", }, { "id": "id_155", "type": "input", "businesstype": { "type": "password" } } ] var filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}] and want common obejct like var output = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "id": "id_16677", "type": "div", } ] How

Can _lodash test an array to check if an array element has a field with a certain value?

六眼飞鱼酱① 提交于 2019-11-30 07:59:16
I have a variable selectedSubTopicId and I have an array of subTopic objects: objectiveDetail.subTopics[] . Each subTopic object has a field subTopicId I would like to use this to enable or disable and Add topic button. Can I use lodash in the ng-disabled to test this array and report true if any subTopic object element of the array has a subTopicId that is equal to the selectedSubTopicId . Here's a sample of the data that's in objectiveDetail. In this case there's just one element in the subTopics array. {"objectiveDetailId":285, "objectiveId":29, "number":1, "text":"x", "subTopics":[{

lodash orderBy on nested property

痴心易碎 提交于 2019-11-30 06:55:39
问题 I'm using v4.11.0 . I would like sort objects based on milliseconds property. Here's the array : [ { "name": "bug12755.xml", "list": "bugs42", "start-date": "2015-09-14", "age": { "text": "7 months", "milliseconds": 18381227304 } }, { "name": "bug12922.xml", "list": "bugs42", "start-date": "2015-08-27", "age": { "text": "8 months", "milliseconds": 19936427304 } }, { "name": "bug13183.xml", "list": "bugs50", "start-date": "2015-08-27", "age": { "text": "8 months", "milliseconds": 19936427305 }

Lodash .clone and .cloneDeep behaviors

白昼怎懂夜的黑 提交于 2019-11-30 06:34:11
问题 I try to clone an array of objects with nested objects. Something like: var data = [ { id: 1, values: { a: 'a', b: 'b' } }, { id: 2, values: { c: 'c', d: 'd' } } ]; _.Clone With the _.clone method and the isDeep parameter at true : var clone = _.clone(data, true); data[1].values.d = 'x'; console.log( _.isEqual(data, clone) ); // true, clone[1].values.d == 'x' I expected clone[1].values.d == 'd' : If isDeep is true nested objects will also be cloned, otherwise they will be assigned by

Fastest method for testing a fixed phone number pattern

余生长醉 提交于 2019-11-30 05:40:42
问题 So, the challenge is that we are trying to detect if a string matches a fixed phone number pattern, this is a simple string pattern. The pattern is: ddd-ddd-dddd Where "d "represents a decimal digit and the minus symbol represents itself, "-" The patterns that are currently used for testing are, but can be increased if it is felt that there are not enough patterns to debunk an incorrect format. "012-345-6789" "0124-345-6789" "012-3456-6789" "012-345-67890" "01a-345-6789" "012-34B-6789" "012

Find Duplicate Array within Array

荒凉一梦 提交于 2019-11-30 05:34:33
问题 Given an array of arrays, what would be the efficient way of identifying the duplicate item? var array = [ [ 11.31866455078125, 44.53836644772605 ], [ // <-- Here's the duplicate 11.31866455078125, 44.53836644772605 ], [ 11.371536254882812, 44.53836644772605 ], [ 11.371536254882812, 44.50140292110874 ] ] I've been working on this with lodash as an accepted dependency, and I get how to just return the "unique" list using _.uniqWith and _.isEqual: _.uniqWith(array,_.isEqual) With would give the

lodash _.get function in typescript

社会主义新天地 提交于 2019-11-30 04:45:18
问题 I get the feeling after some googling that a lot of lodash's functions can be achieved with native typescript but i cannot find a straightforward answer for the _.get function... In lodash the following, using the _.get function alerts 1 let obj = {a:{b:1}}; let a = _.get(obj, 'a.b'); alert(a); Is there a way of achieving the same result with only typescript? 回答1: In pain Javascript you could split the path and reduce the path by walking the given object. function getValue(object, path) {