英文:
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 = [
{
'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,
}
]
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 = [
{
'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,
},
]
答案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 = [{"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)))
<!-- 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 = [
{ '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)
Translated with www.DeepL.com/Translator (free version)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论