将类型引用为索引时出错
Getting error while referencing the type as index
我有一个具有以下结构的对象数组
let sampleData = [
{ valueObj: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
{ time: "14354545454", sum: 0},
{ time: "14354545454", sum: 0} }
];
我需要获取数组中每个对象中的每个键并从中形成一个数组。基本上根据所有 objects.If 中存在的键进行分组对象没有 "values" 它应该 return 0 跨越 val1,val2,val3.
result = [
{ name: 'High', data: [4, 5, 0, 0] },
{ name: 'Medium', data: [5, 3, 0, 0] },
{ name: 'Low', data: [7, 1, 0, 0] }
];
我在这里传递一个字符串,它将在内部使用 reduce.I 收到此错误 any cannot be used as index 每当我传入一个动态字符串时只要启用打字稿,就会在 reduce 函数中使用。
代码工作正常,但在我升级到最新的 TS 后它抛出错误并且无法编译
有人可以帮助我吗?
我试过以下方法:
const sampleData = [{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: "1571372233234", sum: 16 }, { valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234", sum: 9 }, { time: "14354545454", sum: 0 }, { time: "14354545454", sum: 0 }];
const keys = ['High', 'Low', 'Medium'];
function formResult(sampleData, prop, keys){
let grouped = sampleData.reduce((r, { [prop]: values = {} } = {}) => {
r.forEach(({ name, data }) => data.push(values[name] || 0));
return r;
}, keys.map(name => ({ name, data: [] })));
console.log(grouped);
}
formResult(sampleData,"valueObj", keys);
该函数未返回 grouped
。这可能是问题所在
const sampleData = [{
valueObj: {
High: 4,
Low: 5,
Medium: 7
},
time: "1571372233234",
sum: 16
}, {
valueObj: {
High: 5,
Low: 3,
Medium: 1
},
time: "1571372233234",
sum: 9
}, {
time: "14354545454",
sum: 0
}, {
time: "14354545454",
sum: 0
}];
const keys = ['High', 'Low', 'Medium'];
function formResult(sampleData, prop, keys) {
return sampleData.reduce((r, {
[prop]: values = {}
} = {}) => {
r.forEach(({
name,
data
}) => data.push(values[name] || 0));
return r;
}, keys.map(name => ({
name,
data: []
})));
}
console.log(formResult(sampleData, "valueObj", keys));
在 Typescript 中使用带有动态键的 reduce
可能很丑陋,并且在创建单个对象时是 arguably not appropriate even in Javascript。考虑创建将数据放入循环外部的对象 - 一个属性是名称(高等)并且值是数字数组的对象。在每次迭代时推送到数字数组(如果 属性 不存在,则推送 0),如果需要,首先使用数组创建 属性。循环后,将对象转为对象数组:
// compliant with noImplicitAny and strict options
type DataItem = {
time: string;
sum: number;
valueObj?: {
High: number;
Medium: number;
Low: number;
}
};
const sampleData: DataItem[] = [
{ valueObj: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
{ time: "14354545454", sum: 0},
{ time: "14354545454", sum: 0}
];
const keys = ['High', 'Low', 'Medium'] as const;
const grouped = {} as { [key: string]: number[] };
for (const item of sampleData) {
for (const key of keys) {
if (!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item.valueObj ? item.valueObj[key] : 0);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
编译输出:
"use strict";
const sampleData = [
{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: "1571372233234", sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium: 1 }, time: "1571372233234", sum: 9 },
{ time: "14354545454", sum: 0 },
{ time: "14354545454", sum: 0 }
];
const keys = ['High', 'Low', 'Medium'];
const grouped = {};
for (const item of sampleData) {
for (const key of keys) {
if (!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item.valueObj ? item.valueObj[key] : 0);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
console.log(output);
如果 valueObj
键是动态的,它会变得更丑陋。我能弄清楚的是,在遍历数组项时,如果它具有自己的 属性 对象键,则断言数组项的类型为 { [possibleKeyForObj: string]: { [key: string]: number } }
,从而允许您访问嵌套 属性:
const formResult = (
sampleData: object[],
possibleKeyForObj: string,
keys: string[],
) => {
const grouped = Object.fromEntries(keys.map(key => [key, []]));
for (const item of sampleData) {
for (const key of keys) {
grouped[key].push(
item.hasOwnProperty(possibleKeyForObj)
? (item as { [possibleKeyForObj: string]: { [key: string]: number } })[possibleKeyForObj][key]
: 0,
);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
console.log(output);
};
formResult(
[
{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: '1571372233234', sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium: 1 }, time: '1571372233234', sum: 9 },
{ time: '14354545454', sum: 0 },
{ time: '14354545454', sum: 0 },
],
'valueObj',
['High', 'Low', 'Medium'],
);
我有一个具有以下结构的对象数组
let sampleData = [
{ valueObj: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
{ time: "14354545454", sum: 0},
{ time: "14354545454", sum: 0} }
];
我需要获取数组中每个对象中的每个键并从中形成一个数组。基本上根据所有 objects.If 中存在的键进行分组对象没有 "values" 它应该 return 0 跨越 val1,val2,val3.
result = [
{ name: 'High', data: [4, 5, 0, 0] },
{ name: 'Medium', data: [5, 3, 0, 0] },
{ name: 'Low', data: [7, 1, 0, 0] }
];
我在这里传递一个字符串,它将在内部使用 reduce.I 收到此错误 any cannot be used as index 每当我传入一个动态字符串时只要启用打字稿,就会在 reduce 函数中使用。
代码工作正常,但在我升级到最新的 TS 后它抛出错误并且无法编译
有人可以帮助我吗?
我试过以下方法:
const sampleData = [{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: "1571372233234", sum: 16 }, { valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234", sum: 9 }, { time: "14354545454", sum: 0 }, { time: "14354545454", sum: 0 }];
const keys = ['High', 'Low', 'Medium'];
function formResult(sampleData, prop, keys){
let grouped = sampleData.reduce((r, { [prop]: values = {} } = {}) => {
r.forEach(({ name, data }) => data.push(values[name] || 0));
return r;
}, keys.map(name => ({ name, data: [] })));
console.log(grouped);
}
formResult(sampleData,"valueObj", keys);
该函数未返回 grouped
。这可能是问题所在
const sampleData = [{
valueObj: {
High: 4,
Low: 5,
Medium: 7
},
time: "1571372233234",
sum: 16
}, {
valueObj: {
High: 5,
Low: 3,
Medium: 1
},
time: "1571372233234",
sum: 9
}, {
time: "14354545454",
sum: 0
}, {
time: "14354545454",
sum: 0
}];
const keys = ['High', 'Low', 'Medium'];
function formResult(sampleData, prop, keys) {
return sampleData.reduce((r, {
[prop]: values = {}
} = {}) => {
r.forEach(({
name,
data
}) => data.push(values[name] || 0));
return r;
}, keys.map(name => ({
name,
data: []
})));
}
console.log(formResult(sampleData, "valueObj", keys));
在 Typescript 中使用带有动态键的 reduce
可能很丑陋,并且在创建单个对象时是 arguably not appropriate even in Javascript。考虑创建将数据放入循环外部的对象 - 一个属性是名称(高等)并且值是数字数组的对象。在每次迭代时推送到数字数组(如果 属性 不存在,则推送 0),如果需要,首先使用数组创建 属性。循环后,将对象转为对象数组:
// compliant with noImplicitAny and strict options
type DataItem = {
time: string;
sum: number;
valueObj?: {
High: number;
Medium: number;
Low: number;
}
};
const sampleData: DataItem[] = [
{ valueObj: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
{ time: "14354545454", sum: 0},
{ time: "14354545454", sum: 0}
];
const keys = ['High', 'Low', 'Medium'] as const;
const grouped = {} as { [key: string]: number[] };
for (const item of sampleData) {
for (const key of keys) {
if (!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item.valueObj ? item.valueObj[key] : 0);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
编译输出:
"use strict";
const sampleData = [
{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: "1571372233234", sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium: 1 }, time: "1571372233234", sum: 9 },
{ time: "14354545454", sum: 0 },
{ time: "14354545454", sum: 0 }
];
const keys = ['High', 'Low', 'Medium'];
const grouped = {};
for (const item of sampleData) {
for (const key of keys) {
if (!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item.valueObj ? item.valueObj[key] : 0);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
console.log(output);
如果 valueObj
键是动态的,它会变得更丑陋。我能弄清楚的是,在遍历数组项时,如果它具有自己的 属性 对象键,则断言数组项的类型为 { [possibleKeyForObj: string]: { [key: string]: number } }
,从而允许您访问嵌套 属性:
const formResult = (
sampleData: object[],
possibleKeyForObj: string,
keys: string[],
) => {
const grouped = Object.fromEntries(keys.map(key => [key, []]));
for (const item of sampleData) {
for (const key of keys) {
grouped[key].push(
item.hasOwnProperty(possibleKeyForObj)
? (item as { [possibleKeyForObj: string]: { [key: string]: number } })[possibleKeyForObj][key]
: 0,
);
}
}
const output = Object.entries(grouped).map(([name, data]) => ({ name, data }));
console.log(output);
};
formResult(
[
{ valueObj: { High: 4, Low: 5, Medium: 7 }, time: '1571372233234', sum: 16 },
{ valueObj: { High: 5, Low: 3, Medium: 1 }, time: '1571372233234', sum: 9 },
{ time: '14354545454', sum: 0 },
{ time: '14354545454', sum: 0 },
],
'valueObj',
['High', 'Low', 'Medium'],
);