gpt4 book ai didi

javascript - 根据动态传递的属性从对象数组中获取唯一值

转载 作者:行者123 更新时间:2023-11-30 13:57:47 26 4
gpt4 key购买 nike

在这种情况下,我需要根据动态传递的属性获取所有对象的唯一值。我尝试了以下方法,但似乎不起作用。

var arr = [
{
id: "1",
type: "x",
source: {
val1: "3",
val2: "4",
val3: "6",
},
},
{
id: "1",
type: "x",
source: {
val1: "3",
val2: "4",
val3: "6",
},
},
{
id: "1",
type: "x",
source: {
val1: "4",
val2: "5",
val3: "6",
}
}
];

现在假设我通过了 val1,它应该给我唯一值 3,4,如果我通过了 val2,它应该给我 4,5。P.S:我只会传递源属性中存在的参数。

我尝试过的方法:

 calculate = (param) =>
{
let uniqueValues = Array.from(
new Set(arr.map((arr: any) => arr[param]))
);
}

最佳答案

看起来,您还需要 source 属性。

new Set(arr.map((o: any) => o.source[param]))

关于javascript - 根据动态传递的属性从对象数组中获取唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56887792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com