如何 select 具有来自 javascript 数组的不同值的多个特定 属性
How to select multiple specific property with distinct value from javascript array
假设我有一个如下所示的 Javascript 数组。
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
我想获得两个特定属性(group
和 testid
)。
我想在最终结果中检索这些属性的唯一值。
所以我的最终结果将是
{group:A,testid:1},{group:B,testid:2}
目前我已经尝试过的内容如下。
data.map(item=>item.group).
但这只会给我一个 属性 并且没有任何不同的值
如何使用 Javascript
中最新的 ecmascript 语法实现此目的
您可以遍历它并获得所需的结果。
result = []
data.forEach(x=>{
if(!result.some(y=>y.group===x.group && x.testid===y.testid)){
result.push({group:x.group,testid:x.testid});
}
});
您可以 reduce
数组并每次检查是否存在对:
data.reduce((prev, el) =>{
if(prev.some(o => o.group == el.group && o.testid == el.testid))
return prev;
return [...prev, {group:el.group, testid:el.testid}]
}, [])
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
let result = data.reduce((prev, el) =>{
if(prev.some(o => o.group == el.group && o.testid == el.testid))
return prev;
return [...prev, {group:el.group, testid:el.testid}]
}, []);
console.log(result);
使用forEach
循环并构建一个对象,键为uniq_id。
遍历后,上述对象的returnObject.values
const convert = (arr) => {
const res = {};
arr.forEach(({group, testid}) => {
// change uniq_id based on requirement
const uniq_id = `${group}-${testid}`;
res[uniq_id] = { group, testid};
});
return Object.values(res);
}
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
console.log(convert(data));
假设我有一个如下所示的 Javascript 数组。
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
我想获得两个特定属性(group
和 testid
)。
我想在最终结果中检索这些属性的唯一值。 所以我的最终结果将是
{group:A,testid:1},{group:B,testid:2}
目前我已经尝试过的内容如下。
data.map(item=>item.group).
但这只会给我一个 属性 并且没有任何不同的值 如何使用 Javascript
中最新的 ecmascript 语法实现此目的您可以遍历它并获得所需的结果。
result = []
data.forEach(x=>{
if(!result.some(y=>y.group===x.group && x.testid===y.testid)){
result.push({group:x.group,testid:x.testid});
}
});
您可以 reduce
数组并每次检查是否存在对:
data.reduce((prev, el) =>{
if(prev.some(o => o.group == el.group && o.testid == el.testid))
return prev;
return [...prev, {group:el.group, testid:el.testid}]
}, [])
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
let result = data.reduce((prev, el) =>{
if(prev.some(o => o.group == el.group && o.testid == el.testid))
return prev;
return [...prev, {group:el.group, testid:el.testid}]
}, []);
console.log(result);
使用forEach
循环并构建一个对象,键为uniq_id。
遍历后,上述对象的returnObject.values
const convert = (arr) => {
const res = {};
arr.forEach(({group, testid}) => {
// change uniq_id based on requirement
const uniq_id = `${group}-${testid}`;
res[uniq_id] = { group, testid};
});
return Object.values(res);
}
const data = [
{ group: 'A', name: 'SD', testid:1},
{ group: 'B', name: 'FI',testid:2 },
{ group: 'A', name: 'MM', testid:1 },
{ group: 'B', name: 'CO', testid:2 },
{ group: 'A', name: 'QW', testid:1 }
];
console.log(convert(data));