用多个属性的条件筛选数组。

huangapple go评论80阅读模式
英文:

Filter an array with conditions across multiple attributes

问题

以下是要翻译的内容:

输入数组:

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,
    }
]

请注意,我已经将JavaScript代码部分留在原始语言中,只翻译了注释和描述。

英文:

I have an array with parent and child and the expense of the child (There can be more than 3 expenses).
I want to filter the list grouped by parents and show the parents where combination of children has a mix of positive and negative expenses.
Filter out parents if they have only positive or only negative expenses.

Input Array:

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

Output Array: It should only have B and C as they have a combination of positive and negative expense among the children expense.

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

答案1

得分: 1

获取一组唯一的父项。对于每个唯一的父项,找到所有子项,获取所有费用(即属性名称以单词'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)))
英文:

Get a set of unique parents. For each unique parent, locate all children, get all expenses (i.e. where the property name starts with the word 'expense'), extract all the expense values, and check if there is a mix of positive and negative numbers. Then, if the parent meets that test, extract all entries of that parent and include it in the result.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = [{&quot;parent&quot;:&quot;A&quot;,&quot;child&quot;:&quot;RST&quot;,&quot;expense1&quot;:1,&quot;expense2&quot;:2,&quot;expense3&quot;:3},{&quot;parent&quot;:&quot;A&quot;,&quot;child&quot;:&quot;EST&quot;,&quot;expense1&quot;:4,&quot;expense2&quot;:5,&quot;expense3&quot;:6},{&quot;parent&quot;:&quot;A&quot;,&quot;child&quot;:&quot;QST&quot;,&quot;expense1&quot;:4,&quot;expense2&quot;:2,&quot;expense3&quot;:6},{&quot;parent&quot;:&quot;B&quot;,&quot;child&quot;:&quot;EST&quot;,&quot;expense1&quot;:1,&quot;expense2&quot;:2,&quot;expense3&quot;:-3},{&quot;parent&quot;:&quot;B&quot;,&quot;child&quot;:&quot;VST&quot;,&quot;expense1&quot;:6,&quot;expense2&quot;:2,&quot;expense3&quot;:3},{&quot;parent&quot;:&quot;B&quot;,&quot;child&quot;:&quot;NST&quot;,&quot;expense1&quot;:3,&quot;expense2&quot;:8,&quot;expense3&quot;:7},{&quot;parent&quot;:&quot;C&quot;,&quot;child&quot;:&quot;UST&quot;,&quot;expense1&quot;:-8,&quot;expense2&quot;:-2,&quot;expense3&quot;:3},{&quot;parent&quot;:&quot;C&quot;,&quot;child&quot;:&quot;PST&quot;,&quot;expense1&quot;:-6,&quot;expense2&quot;:-5,&quot;expense3&quot;:-3},{&quot;parent&quot;:&quot;C&quot;,&quot;child&quot;:&quot;LST&quot;,&quot;expense1&quot;:-3,&quot;expense2&quot;:-8,&quot;expense3&quot;:-7},{&quot;parent&quot;:&quot;D&quot;,&quot;child&quot;:&quot;WST&quot;,&quot;expense1&quot;:-8,&quot;expense2&quot;:-2,&quot;expense3&quot;:-3},{&quot;parent&quot;:&quot;D&quot;,&quot;child&quot;:&quot;CST&quot;,&quot;expense1&quot;:-6,&quot;expense2&quot;:-5,&quot;expense3&quot;:-3},{&quot;parent&quot;:&quot;B&quot;,&quot;child&quot;:&quot;KST&quot;,&quot;expense1&quot;:-3,&quot;expense2&quot;:-8,&quot;expense3&quot;:-7}]
console.log([...new Set(data.map(i=&gt;i.parent))]
.filter(parent=&gt;data.filter(i=&gt;i.parent===parent)
.flatMap(i=&gt;Object.entries(i).filter(([k])=&gt;k.startsWith(&#39;expense&#39;))
.map(([k,v])=&gt;Math.abs(v)===v))
.reduce((a,c,i,r)=&gt;a || i&gt;0 &amp;&amp; c!==r[i-1],false))
.flatMap(parent=&gt;data.filter(i=&gt;i.parent===parent)))

<!-- end snippet -->

答案2

得分: 1

过滤数组在你不了解高阶函数(https://eloquentjavascript.net/05_higher_order.html)的情况下可能会有点棘手。基本上,它们抽象了我们可以使用 if 循环和 else 语句来移除或添加我们想要的代码。

我不太明白你的问题,因为我生成的输出与你想要的不同,但我坚持要消除那些只有正数或只有负数的行。

幸运的是,这不是一个问题,因为你可以在我留下来注释的函数部分实现自己的逻辑。

基本上,所做的是使用数组的本地函数 filter,并为其中的每一行添加一个逻辑,如果满足该逻辑,则返回该行到我想要的最终数组。

完整代码:

// 输入
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 }
]

// 输出
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 ( // 添加逻辑以包括或排除行
    (rowOfInput.expense1 < 0 && rowOfInput.expense2 < 0 && rowOfInput.expense3 < 0) ||
    (rowOfInput.expense1 > 0 && rowOfInput.expense2 > 0 && rowOfInput.expense3 > 0)
  ) return rowOfInput // 如果是你想要的,返回该行
}

let inputFiltered = input.filter(filterLogic)

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

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

英文:

Filtering arrays can be a bit tricky when you don't have a grasp on higher order functions (https://eloquentjavascript.net/05_higher_order.html). Basically they abstract away the code that we could do using if loops and elses to remove or add what we want.

I don't quite understand your question, because the output I did is different from what you want, but I stuck to eliminating all that were only positive or only negative.

Fortunately this is not a problem because you can implement your own logic in the part of the function that I left commented out for this.

Basically what was done was to use the native function of arrays, filter and add a logic for each row of it and if this logic is met, return that row to the final array that I want.

Complete code:

// Input
let input = [
{ &#39;parent&#39;: &#39;A&#39;, &#39;child&#39;: &#39;RST&#39;, &#39;expense1&#39;: 1, &#39;expense2&#39;: 2, &#39;expense3&#39;: 3 },
{ &#39;parent&#39;: &#39;A&#39;, &#39;child&#39;: &#39;EST&#39;, &#39;expense1&#39;: 4 4, &#39;expense2&#39;: 5, &#39;expense3&#39;: 6 },
{ &#39;parent&#39;: &#39;A&#39;, &#39;child&#39;: &#39;QST&#39;, &#39;expense1&#39;: 4, &#39;expense2&#39;: 2, &#39;expense3&#39;: 6 },
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;EST&#39;, &#39;expense1&#39;: 1, &#39;expense2&#39;: 2, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;VST&#39;, &#39;expense1&#39;: 6, &#39;expense2&#39;: 2, &#39;expense3&#39;: 3 },
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;NST&#39;, &#39;expense1&#39;: 3, &#39;expense2&#39;: 8, &#39;expense3&#39;: 7 },
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;UST&#39;, &#39;expense1&#39;: -8, &#39;expense2&#39;: -2, &#39;expense3&#39;: 3 },
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;PST&#39;, &#39;expense1&#39;: -6, &#39;expense2&#39;: -5, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;LST&#39;, &#39;expense1&#39;: -3, &#39;expense2&#39;: -8, &#39;expense3&#39;: -7 }
{ &#39;parent&#39;: &#39;D&#39;, &#39;child&#39;: &#39;WST&#39;, &#39;expense1&#39;: -8, &#39;expense2&#39;: -2, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;D&#39;, &#39;child&#39;: &#39;CST&#39;, &#39;expense1&#39;: -6, &#39;expense2&#39;: -5, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;KST&#39;, &#39;expense1&#39;: -3, &#39;expense2&#39;: -8, &#39;expense3&#39;: -7 }
]
// Output
let output = [
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;EST&#39;, &#39;expense1&#39;: 1, &#39;expense2&#39;: 2, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;VST&#39;, &#39;expense1&#39;: 6, &#39;expense2&#39;: 2, &#39;expense3&#39;: 3 },
{ &#39;parent&#39;: &#39;B&#39;, &#39;child&#39;: &#39;NST&#39;, &#39;expense1&#39;: 3, &#39;expense2&#39;: 8, &#39;expense3&#39;: 7 },
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;UST&#39;, &#39;expense1&#39;: -8, &#39;expense2&#39;: -2, &#39;expense3&#39;: 3 },
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;PST&#39;, &#39;expense1&#39;: -6, &#39;expense2&#39;: -5, &#39;expense3&#39;: -3 }
{ &#39;parent&#39;: &#39;C&#39;, &#39;child&#39;: &#39;LST&#39;, &#39;expense1&#39;: -3, &#39;expense2&#39;: -8, &#39;expense3&#39;: -7 },
]
const filterLogic = rowOfInput =&gt; {
if ( // Add logic to include or exclude line
(rowOfInput.expense1 &lt; 0 &amp;&amp; rowOfInput.expense2 &lt; 0 &amp;&amp; rowOfInput.expense3 &lt; 0) ||
(rowOfInput.expense1 &gt; 0 &amp;&amp; rowOfInput.expense2 &gt; 0 &amp;&amp; rowOfInput.expense3 &gt; 0)
) return rowOfInput // if is your desired, return that line
}
let inputFiltered = input.filter(filterLogic)
console.log(output)
console.log(inputFiltered)

Translated with www.DeepL.com/Translator (free version)

huangapple
  • 本文由 发表于 2023年2月8日 23:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定