如何将数组的值转换为过滤后的数组
How to convert values of array to array filtered
我有这个问题,我用的是Angular8,希望大家能帮帮我,好久没解决
我有这个数组:
datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
},
]
我需要像这样过滤和分组它们:
result = [
{ADMON: {0: 1,
1: 2,
2: 3,
3: 4}},
{USER: {0: 1,
1: 2,
2: 4}}
];
试了很多方法都不行,希望大家能帮帮我,先谢谢了。
尝试使用这样的东西:
前段时间发现了这个功能,用了很多次:
let groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
如果您在您的场景中尝试:
let groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
let datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
},
]
console.log(groupBy(datos, 'Type'));
这是一个可能的解决方案,它使用多种数组方法来完成手头的任务。
const datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
}
];
const grouped = datos.reduce((acc, el) => {
if (!acc[el.Type]) acc[el.Type] = {};
const index = Object.values(acc[el.Type]).length;
acc[el.Type] = {
...acc[el.Type],
[index]: el.Value
}
return acc;
}, {});
const final = Object.entries(grouped).map(([key, val]) => {
return { [key]: val }
});
console.log(final);
const datos = [{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'3'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'4'}];
let result = Object.entries(datos.reduce((acc, {Type, Value}) => {
(acc[Type] = acc[Type] || []).push(Value);
return acc;
}, {}))
.map((o) => { return {[o[0]]: {...o[1]}}});
console.log(result);
我可能会这样做:
// First pass reduce to a map of your index / value maps keyed by type.
// Grouping tends to be easier in a map than an array.
const groupByType = datos.reduce((result, d, i) => {
result[d.Type] = result[d.Type] || {};
result[d.Type][i] = d.Value;
return result;
}, {} as Record<string, Record<string, string>>);
// Once we have a map, we can convert it to the final array
const arrayResult = Object.keys(groupByType).map(t => {
return {
[t]: groupByType[t]
};
});
值得一提的是,我有意没有在 reduce 回调中使用 spread 语法。在许多情况下,这样做可能没问题,但如果您的数据集足够大,则可能会对性能产生影响,因为您将在每次迭代时创建 + 复制。由于创建对象和设置其属性完全封装在 reduce 函数中,因此与其他可能关注不可变性的情况相比,不应该担心变异。
我有这个问题,我用的是Angular8,希望大家能帮帮我,好久没解决
我有这个数组:
datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
},
]
我需要像这样过滤和分组它们:
result = [
{ADMON: {0: 1,
1: 2,
2: 3,
3: 4}},
{USER: {0: 1,
1: 2,
2: 4}}
];
试了很多方法都不行,希望大家能帮帮我,先谢谢了。
尝试使用这样的东西:
前段时间发现了这个功能,用了很多次:
let groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
如果您在您的场景中尝试:
let groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
let datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
},
]
console.log(groupBy(datos, 'Type'));
这是一个可能的解决方案,它使用多种数组方法来完成手头的任务。
const datos = [{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'ADMON',
Value: '3'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '1'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '2'
},
{
Description: 'Puede Insertar',
Id: 1,
Type: 'USER',
Value: '4'
}
];
const grouped = datos.reduce((acc, el) => {
if (!acc[el.Type]) acc[el.Type] = {};
const index = Object.values(acc[el.Type]).length;
acc[el.Type] = {
...acc[el.Type],
[index]: el.Value
}
return acc;
}, {});
const final = Object.entries(grouped).map(([key, val]) => {
return { [key]: val }
});
console.log(final);
const datos = [{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'3'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'4'}];
let result = Object.entries(datos.reduce((acc, {Type, Value}) => {
(acc[Type] = acc[Type] || []).push(Value);
return acc;
}, {}))
.map((o) => { return {[o[0]]: {...o[1]}}});
console.log(result);
我可能会这样做:
// First pass reduce to a map of your index / value maps keyed by type.
// Grouping tends to be easier in a map than an array.
const groupByType = datos.reduce((result, d, i) => {
result[d.Type] = result[d.Type] || {};
result[d.Type][i] = d.Value;
return result;
}, {} as Record<string, Record<string, string>>);
// Once we have a map, we can convert it to the final array
const arrayResult = Object.keys(groupByType).map(t => {
return {
[t]: groupByType[t]
};
});
值得一提的是,我有意没有在 reduce 回调中使用 spread 语法。在许多情况下,这样做可能没问题,但如果您的数据集足够大,则可能会对性能产生影响,因为您将在每次迭代时创建 + 复制。由于创建对象和设置其属性完全封装在 reduce 函数中,因此与其他可能关注不可变性的情况相比,不应该担心变异。