慕田峪4524236
const convertedDataTest = [ { S1: [ 0, 0, 0, 1 ], C1: [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ], S2: [ 0, 1, 0, 0 ], C2: [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], S3: [ 1, 0, 0, 0 ], C3: [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], S4: [ 0, 0, 1, 0 ], C4: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ], S5: [ 0, 1, 0, 0 ], C5: [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ], CLASS: [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] } ]convertedDataTest是一个只有1个对象的数组,该对象在索引0处可用。因此,当您尝试从索引1获取值时,它返回未定义的值。您可以通过此行获取类数组convertedDataTest[0].CLASS
FFIVE
您的代码似乎将数据包装在一个额外的数组中,因此convertedData[0][0].CLASS可以进行定义。如果从convertData函数的return语句中删除数组,则如下所示convertedData[0].CLASS。var data = [ { "S1": 4, "C1": 11, "S2": 2, "C2": 9, "S3": 1, "C3": 5, "S4": 3, "C4": 9, "S5": 2, "C5": 7, "CLASS": 1 }, { "S1": 4, "C1": 5, "S2": 2, "C2": 3, "S3": 1, "C3": 1, "S4": 3, "C4": 10, "S5": 2, "C5": 6, "CLASS": 0 }, { "S1": 3, "C1": 8, "S2": 2, "C2": 7, "S3": 2, "C3": 3, "S4": 3, "C4": 7, "S5": 1, "C5": 12, "CLASS": 1 }, { "S1": 3, "C1": 12, "S2": 4, "C2": 8, "S3": 4, "C3": 7, "S4": 3, "C4": 3, "S5": 1, "C5": 6, "CLASS": 0 }, { "S1": 1, "C1": 2, "S2": 2, "C2": 12, "S3": 1, "C3": 8, "S4": 1, "C4": 13, "S5": 3, "C5": 1, "CLASS": 0 }, { "S1": 2, "C1": 7, "S2": 2, "C2": 5, "S3": 4, "C3": 9, "S4": 4, "C4": 6, "S5": 1, "C5": 9, "CLASS": 1 }, { "S1": 2, "C1": 2, "S2": 1, "C2": 13, "S3": 2, "C3": 13, "S4": 4, "C4": 3, "S5": 4, "C5": 13, "CLASS": 3 }, { "S1": 4, "C1": 10, "S2": 4, "C2": 11, "S3": 1, "C3": 10, "S4": 2, "C4": 9, "S5": 4, "C5": 1, "CLASS": 1 }, { "S1": 4, "C1": 2, "S2": 3, "C2": 10, "S3": 4, "C3": 7, "S4": 4, "C4": 10, "S5": 2, "C5": 6, "CLASS": 1 }, { "S1": 2, "C1": 8, "S2": 4, "C2": 13, "S3": 3, "C3": 8, "S4": 3, "C4": 2, "S5": 4, "C5": 2, "CLASS": 2 }];function arrayMaker(length, val) { let arr = []; for (let i = 0; i < length; i++) { if (i == val - 1 || (i == 0 && val == 0)) arr.push(1); else arr.push(0); } return arr;}function convertData(data) { const convertedData = data.map(item => { return { S1: arrayMaker(4, item.S1), C1: arrayMaker(13, item.C1), S2: arrayMaker(4, item.S2), C2: arrayMaker(13, item.C2), S3: arrayMaker(4, item.S3), C3: arrayMaker(13, item.C3), S4: arrayMaker(4, item.S4), C4: arrayMaker(13, item.C4), S5: arrayMaker(4, item.S5), C5: arrayMaker(13, item.C5), CLASS: arrayMaker(10, item.CLASS) }; }); return convertedData;}const convertedData = convertData(data);console.log(convertedData[0].CLASS);