Lodash打字稿 - 将多个阵列组合到一个单个数组中,并对每个数组进行计算

发布于 2025-02-10 06:39:13 字数 902 浏览 1 评论 0原文

我有一个记录< string,number [] []>并试图对这些值进行计算。

一个示例输入:

   const input1 = {
      key1: [
        [2002, 10],
        [2003, 50],
      ],
    };
    const input2 = {
      key1: [
        [2002, 20],
        [2003, 70],
      ],
    };
    const input3 = {
      key1: [
        [2002, 5],
        [2003, 60],
      ],
    };

对于每个键,对于特定年份,我想

year => input1 + input2 - input3
// output: 2002 => 25, 2003 => 60

对此进行以下操作,我一直在使用Lodash/fp。

map(a => a.map(nth(1)))(map('key1')([input1, input2]))
// [[10, 50], [20, 70], [5, 60]]

有没有办法以某种方式传递输入并在它们上迭代,并以某种方式获得回调函数以获取执行计算的值。

我尝试了zipzipwith,但没有到达任何地方。

在这种情况下,我能做什么线索?

感谢您的帮助。

I have a Record<string, number[][]> and trying to perform calculation over these values.

An example input:

   const input1 = {
      key1: [
        [2002, 10],
        [2003, 50],
      ],
    };
    const input2 = {
      key1: [
        [2002, 20],
        [2003, 70],
      ],
    };
    const input3 = {
      key1: [
        [2002, 5],
        [2003, 60],
      ],
    };

For each key, and for the specific year I want to do the following

year => input1 + input2 - input3
// output: 2002 => 25, 2003 => 60

For this I have been playing around with lodash/fp.

map(a => a.map(nth(1)))(map('key1')([input1, input2]))
// [[10, 50], [20, 70], [5, 60]]

is there a way to somehow pass the inputs and iterate over them, and somehow get a callback function to get the values to perform the calculation.

I tried zip, zipWith but didn’t get to anywhere with them.

Any clue what I can do in this instance?

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2025-02-17 06:39:13

您可以使用_。frompairs方法获得一个对象,然后使用_。Mergewith添加和减去对象。

const input1 = {
  key1: [
    [2002, 10],
    [2003, 50],
  ],
};
const input2 = {
  key1: [
    [2002, 20],
    [2003, 70],
  ],
};
const input3 = {
  key1: [
    [2002, 5],
    [2003, 60],
  ],
};


const [one, two, three] = _.map([input1, input2, input3], ({key1 }) => _.fromPairs(key1))
const add = _.mergeWith(one, two, (a, b) => a + b)
const sub = _.mergeWith(add, three, (a, b) => a - b)

console.log(add)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

You could get an object with _.fromPairs method and then use _.mergeWith to add and subtract.

const input1 = {
  key1: [
    [2002, 10],
    [2003, 50],
  ],
};
const input2 = {
  key1: [
    [2002, 20],
    [2003, 70],
  ],
};
const input3 = {
  key1: [
    [2002, 5],
    [2003, 60],
  ],
};


const [one, two, three] = _.map([input1, input2, input3], ({key1 }) => _.fromPairs(key1))
const add = _.mergeWith(one, two, (a, b) => a + b)
const sub = _.mergeWith(add, three, (a, b) => a - b)

console.log(add)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文