- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
美好的一天,
这是我的数据架构:
数据长度可能会根据查询而变化
var data = {
IP1: 1,2,3,4,5,6
IP2: 4,5,6,7,8,9
IP3: 1,7,8,5,9,6,3}
keys=Object.keys(data)
sum=[]
for (i=0;i<keys.length;i++){
k='o.'+keys[i]
sum.push(_.sumBy(data, function(o) { return k }))
}
我正在 Skywise/palantir 上工作,有时我需要使用 loDash sumBy
函数对所有列求和
我的问题:有没有一种方法可以将 o.key[i] 作为 lodash 函数中的变量?
感谢您的帮助。
问候
最佳答案
如果我理解正确的话,您正在尝试获取列的总和(例如 1, 4, 1)。
您可以使用 lodash 创建一个带有 _.flow()
的函数,该函数获取对象(数组)的值,并使用 _ 转置它们(行到列)。 unzip()
,然后通过将其映射到 _.sum()
来对每一列求和:
const { flow, values, unzip, map, sum } = _
const fn = flow(
values, // get the values of the object - a list of arrays
unzip, // unzip to transpose the arrays to columns
arrs => map(arrs, sum) // sum each column
)
const data = {
IP1: [1, 2, 3, 4, 5, 6],
IP2: [4, 5, 6, 7, 8, 9],
IP3: [1, 7, 8, 5, 9, 6, 3]
}
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
还有稍微干净一点的 lodash/fp 版本:
const { flow, values, unzip, map, sum } = _
const fn = flow(
values, // get the values of the object - a list of arrays
unzip, // unzip to transpose the arrays to columns
map(sum) // sum each column
)
const data = {
IP1: [1, 2, 3, 4, 5, 6],
IP2: [4, 5, 6, 7, 8, 9],
IP3: [1, 7, 8, 5, 9, 6, 3]
}
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
关于javascript - 如何在 loDash 函数中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60484619/
使用lodash,如何将生日相同的人的姓名归为一组,如下所示? 我使用嵌套的for循环写出了这个信息,但我想在lodash中会有一种更优雅的方法。我没有使用太多,并试图找出要使用的功能。 [
嘿,我想使用 lodash 计算对象数组中一个属性的总和 假设对象数组看起来像这样...... salary":[{ "bills":[{"electricity":300,"milk":500
我试图在调整窗口大小时触发一个事件,它似乎不起作用。 $(window).resize(function(){ _.debounce(function(){ console.log
我正在使用 StencilJS 编写一个网络组件。由我的组件导入的辅助类之一 (ArrayHelper.ts) 在顶部有这些导入: import transform from 'lodash/tran
我想用比较器对数组进行排序,例如: function myCustomComparator(a, b){...} 好像是 var sorted = myArray.sort(myCustomCompa
我试图弄清楚 Lodash iteratee 的工作原理以及我将在哪里使用它。 The documentation says: Creates a function that invokes func
我是 lodash (v3.10.1) 的新手,很难理解。希望有人能帮忙。 我有这样的输入: { {"id":1,"name":"Matthew","company":{"id":1,"na
我正在尝试添加到流类型的类型中。我从 lodash/fp 模块开始,因为它对我自己最有用。但是,我正在为如何正确输入它而苦苦挣扎。 如果 dropRightWhile 的一个简单示例: declare
有没有办法获取 lodash API 文档的 2.x 版本?上周末似乎主要链接已切换到 3.x,无法查看 2.x 文档。 https://lodash.com/docs 最佳答案 您可以在 Inter
为了减少 Angular 项目的包大小,我正在安装和导入单独的 lodash 库。 IE。 lodash.clonedeep 和其他 lodash.* 但会丢失这些的类型定义,因为它们不能与 @typ
像这样从 lodash 导入"template"有什么区别: import { template } from 'lodash'; 或者像这样: import template from 'lodas
我看到导入整个 lodash 库占用了相当多的磁盘空间: $ du . | grep lodash 1696 ./lodash/fp 5000 ./lodash 在我的代码中我只是在做 r
According to the lodash docs , _.extend(object, [sources]) 改变第一个参数。 var dest = { a: 1 }; _.extend(
我想创建值为“str”的键数组我有以下嵌套 json: [ { "Attr": [ { "power": { "p1": "str", "t3": "str"
lodash 提供了一个方法 _.uniq()从数组中查找唯一元素,但使用的比较函数是严格相等 === , 而我想使用 _.isEqual() ,满足: _.isEqual([1, 2], [1, 2
是escape和 unescape underscore 的功能和 lodash相同?我也可以吗escape与 lodash和 unescape与 underscore并且总是得到相同的字符串? 我可
我知道 rxjs 和 lodash 的定义和职责,但我想知道: 当我在我的项目中使用 rxjs 时,我可以扔掉 lodash 吗? 因为 rxjs 可以同步和异步工作(异步和同步数据)。我认为它可以作
使用 lodash 我想找到一个 id 为 3229 的团队。我尝试关注但它没有返回任何内容。 var team = _.chain(data.teams) .flatten(
我想知道 2 次导入在内存和性能方面是否有任何差异。 如果我的节点模块中有 lodash,无论导入如何,它都会编译所有文件吗? 最佳答案 理论上,based on the specification
基本上,我有一个对象数组,我只想更新数组中满足条件的对象。我想知道是否有解决该问题的有效方法。现在我正在使用 lodash。这是一个例子: var things = [ {id: 1, typ
我是一名优秀的程序员,十分优秀!