typescript 使用跨多个属性的条件筛选数组

eagi6jfj  于 2023-02-10  发布在  TypeScript
关注(0)|答案(2)|浏览(200)

我有一个包含父项和子项以及子项费用的数组(可以有3个以上的费用)。我想过滤按父项分组的列表,并向父项显示子项组合中混合了正费用和负费用的位置。如果父项只有正费用或负费用,则将其过滤掉。

输入数组:

let inputArray1 = [
    {
        'parent':'A',
        'child':'RST',
        'expense1':1,
        'expense2':2,
        'expense3':3,
    },
    {
        'parent':'A',
        'child':'EST',
        'expense1':4,
        'expense2':5,
        'expense3':6,
    },
    {
        'parent':'A',
        'child':'QST',
        'expense1':4,
        'expense2':2,
        'expense3':6,
    },
    {
        'parent':'B',
        'child':'EST',
        'expense1':1,
        'expense2':2,
        'expense3':-3,  //. <----Negative Expense
    },
    {
        'parent':'B',
        'child':'VST',
        'expense1':6,
        'expense2':2,
        'expense3':3,
    },
    {
        'parent':'B',
        'child':'NST',
        'expense1':3,
        'expense2':8,
        'expense3':7,
    },
    {
        'parent':'C',
        'child':'UST',
        'expense1':-8,
        'expense2':-2,
        'expense3':3,  //<--- Positive Expense
    },
    {
        'parent':'C',
        'child':'PST',
        'expense1':-6,
        'expense2':-5,
        'expense3':-3,
    },
    {
        'parent':'C',
        'child':'LST',
        'expense1':-3,
        'expense2':-8,
        'expense3':-7,
    },
    {
        'parent':'D',
        'child':'WST',
        'expense1':-8,
        'expense2':-2,
        'expense3':-3,
    },
    {
        'parent':'D',
        'child':'CST',
        'expense1':-6,
        'expense2':-5,
        'expense3':-3,
    },
    {
        'parent':'D',
        'child':'KST',
        'expense1':-3,
        'expense2':-8,
        'expense3':-7,
    }
]

**输出数组:**它应该只有B和C,因为它们在子费用中具有正费用和负费用的组合。

let inputArray1 = [
        
        {
            'parent':'B',
            'child':'EST',
            'expense1':1,
            'expense2':2,
            'expense3':-3,
        },
        {
            'parent':'B',
            'child':'VST',
            'expense1':6,
            'expense2':2,
            'expense3':3,
        },
        {
            'parent':'B',
            'child':'NST',
            'expense1':3,
            'expense2':8,
            'expense3':7,
        },
        {
            'parent':'C',
            'child':'UST',
            'expense1':-8,
            'expense2':-2,
            'expense3':3,
        },
        {
            'parent':'C',
            'child':'PST',
            'expense1':-6,
            'expense2':-5,
            'expense3':-3,
        },
        {
            'parent':'C',
            'child':'LST',
            'expense1':-3,
            'expense2':-8,
            'expense3':-7,
        },
        
    ]
von4xj4u

von4xj4u1#

获取一组唯一的父项。对于每个唯一的父项,查找所有子项,获取所有费用(即财产名称以单词“expense”开头的地方),提取所有费用值,并检查是否存在正数和负数的混合。然后,如果父项满足该测试,则提取该父项的所有条目并将其包括在结果中。

const data = [{"parent":"A","child":"RST","expense1":1,"expense2":2,"expense3":3},{"parent":"A","child":"EST","expense1":4,"expense2":5,"expense3":6},{"parent":"A","child":"QST","expense1":4,"expense2":2,"expense3":6},{"parent":"B","child":"EST","expense1":1,"expense2":2,"expense3":-3},{"parent":"B","child":"VST","expense1":6,"expense2":2,"expense3":3},{"parent":"B","child":"NST","expense1":3,"expense2":8,"expense3":7},{"parent":"C","child":"UST","expense1":-8,"expense2":-2,"expense3":3},{"parent":"C","child":"PST","expense1":-6,"expense2":-5,"expense3":-3},{"parent":"C","child":"LST","expense1":-3,"expense2":-8,"expense3":-7},{"parent":"D","child":"WST","expense1":-8,"expense2":-2,"expense3":-3},{"parent":"D","child":"CST","expense1":-6,"expense2":-5,"expense3":-3},{"parent":"B","child":"KST","expense1":-3,"expense2":-8,"expense3":-7}]

console.log([...new Set(data.map(i=>i.parent))]
  .filter(parent=>data.filter(i=>i.parent===parent)
  .flatMap(i=>Object.entries(i).filter(([k])=>k.startsWith('expense'))
  .map(([k,v])=>Math.abs(v)===v))
  .reduce((a,c,i,r)=>a || i>0 && c!==r[i-1],false))
  .flatMap(parent=>data.filter(i=>i.parent===parent)))
omtl5h9j

omtl5h9j2#

当你不了解高阶函数(https://eloquentjavascript.net/05_higher_order.html)的时候,过滤数组可能会有点棘手,基本上它们抽象了我们可以使用if循环和elses来删除或添加我们想要的代码。
我不太明白你的问题,因为我做的输出和你想要的不一样,但我坚持排除所有只有正面或只有负面的东西。
幸运的是,这不是一个问题,因为您可以在函数中我为此注解掉的部分实现自己的逻辑。
基本上我们所做的就是使用数组的原生函数,过滤并为它的每一行添加一个逻辑,如果满足这个逻辑,则将该行返回到我想要的最终数组。
完整代码:

// Input
let input = [
  { 'parent': 'A', 'child': 'RST', 'expense1': 1, 'expense2': 2, 'expense3': 3 },
  { 'parent': 'A', 'child': 'EST', 'expense1': 4 4, 'expense2': 5, 'expense3': 6 },
  { 'parent': 'A', 'child': 'QST', 'expense1': 4, 'expense2': 2, 'expense3': 6 },
  { 'parent': 'B', 'child': 'EST', 'expense1': 1, 'expense2': 2, 'expense3': -3 }
  { 'parent': 'B', 'child': 'VST', 'expense1': 6, 'expense2': 2, 'expense3': 3 },
  { 'parent': 'B', 'child': 'NST', 'expense1': 3, 'expense2': 8, 'expense3': 7 },
  { 'parent': 'C', 'child': 'UST', 'expense1': -8, 'expense2': -2, 'expense3': 3 },
  { 'parent': 'C', 'child': 'PST', 'expense1': -6, 'expense2': -5, 'expense3': -3 }
  { 'parent': 'C', 'child': 'LST', 'expense1': -3, 'expense2': -8, 'expense3': -7 }
  { 'parent': 'D', 'child': 'WST', 'expense1': -8, 'expense2': -2, 'expense3': -3 }
  { 'parent': 'D', 'child': 'CST', 'expense1': -6, 'expense2': -5, 'expense3': -3 }
  { 'parent': 'B', 'child': 'KST', 'expense1': -3, 'expense2': -8, 'expense3': -7 }
]

// Output
let output = [
  { 'parent': 'B', 'child': 'EST', 'expense1': 1, 'expense2': 2, 'expense3': -3 }
  { 'parent': 'B', 'child': 'VST', 'expense1': 6, 'expense2': 2, 'expense3': 3 },
  { 'parent': 'B', 'child': 'NST', 'expense1': 3, 'expense2': 8, 'expense3': 7 },
  { 'parent': 'C', 'child': 'UST', 'expense1': -8, 'expense2': -2, 'expense3': 3 },
  { 'parent': 'C', 'child': 'PST', 'expense1': -6, 'expense2': -5, 'expense3': -3 }
  { 'parent': 'C', 'child': 'LST', 'expense1': -3, 'expense2': -8, 'expense3': -7 },
]

const filterLogic = rowOfInput => {
  if ( // Add logic to include or exclude line
    (rowOfInput.expense1 < 0 && rowOfInput.expense2 < 0 && rowOfInput.expense3 < 0) ||
    (rowOfInput.expense1 > 0 && rowOfInput.expense2 > 0 && rowOfInput.expense3 > 0)
  ) return rowOfInput // if is your desired, return that line
}

let inputFiltered = input.filter(filterLogic)

console.log(output)
console.log(inputFiltered)

www.DeepL.com/Translator翻译(免费版)

相关问题