猿问

数组中的对象值未定义

我需要从数组对象中获取价值。但是,如果直接调用它,我将无法定义。我在下面写示例和代码。


因此,我创建了对象数组。例子:


const convertedDataTest = [ [ { S1: [Array],

      C1: [Array],

      S2: [Array],

      C2: [Array],

      S3: [Array],

      C3: [Array],

      S4: [Array],

      C4: [Array],

      S5: [Array],

      C5: [Array],

      CLASS: [Array] } ],

  [ { S1: [Array],

      C1: [Array],

      S2: [Array],

      C2: [Array],

      S3: [Array],

      C3: [Array],

      S4: [Array],

      C4: [Array],

      S5: [Array],

      C5: [Array],

      CLASS: [Array] } ],

  [ { S1: [Array],

      C1: [Array],

      S2: [Array],

      C2: [Array],

      S3: [Array],

      C3: [Array],

      S4: [Array],

      C4: [Array],

      S5: [Array],

      C5: [Array],

      CLASS: [Array] } ] ]

如果我调用数组值


console.log(convertedDataTest[1]);

我看到了所有内容,但是如果我调用对象值:


console.log(convertedDataTest[1].CLASS);

我不确定。


我还使用map方法和一些函数创建了这个对象数组。


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);

const convertedDataTest = convertData(dataTest);


蝴蝶刀刀
浏览 234回答 3
3回答

慕运维8079593

您没有在创建对象数组。您将在中创建对象数组.map(...)。map() 使用返回的元素创建一个新数组,然后您将在地图中返回以下内容:return [ // Returning an array.&nbsp; &nbsp; {...}];所以最终的数组将是这样的:[&nbsp; &nbsp; [ // The array returned in map&nbsp; &nbsp; &nbsp; &nbsp; {S1: ..., CLASS: ...}&nbsp; &nbsp; ],&nbsp; &nbsp; [ // The array returned in map&nbsp; &nbsp; &nbsp; &nbsp; {S1: ..., CLASS: ...}&nbsp; &nbsp; ]]注意每个数组元素不是一个对象,而是另一个包含对象的数组。您必须更改.map()为:function convertData(data) {&nbsp; &nbsp; const convertedData = data.map(item => {&nbsp; &nbsp; &nbsp; &nbsp; return { // Note how I removed the [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S1: arrayMaker(4, item.S1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C1: arrayMaker(13, item.C1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S2: arrayMaker(4, item.S2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2: arrayMaker(13, item.C2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S3: arrayMaker(4, item.S3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C3: arrayMaker(13, item.C3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S4: arrayMaker(4, item.S4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C4: arrayMaker(13, item.C4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S5: arrayMaker(4, item.S5),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C5: arrayMaker(13, item.C5),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLASS: arrayMaker(10, item.CLASS)&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; // Note how I removed the ]&nbsp; &nbsp; });&nbsp; &nbsp; return convertedData;}请注意如何删除[和,]然后只返回所需的对象。

慕田峪4524236

const convertedDataTest = [ { S1: [ 0, 0, 0, 1 ],&nbsp; &nbsp; C1: [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],&nbsp; &nbsp; S2: [ 0, 1, 0, 0 ],&nbsp; &nbsp; C2: [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],&nbsp; &nbsp; S3: [ 1, 0, 0, 0 ],&nbsp; &nbsp; C3: [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],&nbsp; &nbsp; S4: [ 0, 0, 1, 0 ],&nbsp; &nbsp; C4: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ],&nbsp; &nbsp; S5: [ 0, 1, 0, 0 ],&nbsp; &nbsp; C5: [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],&nbsp; &nbsp; 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 = [&nbsp; &nbsp; { "S1": 4, "C1": 11, "S2": 2, "C2": 9, "S3": 1, "C3": 5, "S4": 3, "C4": 9, "S5": 2, "C5": 7, "CLASS": 1 },&nbsp; &nbsp; { "S1": 4, "C1": 5, "S2": 2, "C2": 3, "S3": 1, "C3": 1, "S4": 3, "C4": 10, "S5": 2, "C5": 6, "CLASS": 0 },&nbsp; &nbsp; { "S1": 3, "C1": 8, "S2": 2, "C2": 7, "S3": 2, "C3": 3, "S4": 3, "C4": 7, "S5": 1, "C5": 12, "CLASS": 1 },&nbsp; &nbsp; { "S1": 3, "C1": 12, "S2": 4, "C2": 8, "S3": 4, "C3": 7, "S4": 3, "C4": 3, "S5": 1, "C5": 6, "CLASS": 0 },&nbsp; &nbsp; { "S1": 1, "C1": 2, "S2": 2, "C2": 12, "S3": 1, "C3": 8, "S4": 1, "C4": 13, "S5": 3, "C5": 1, "CLASS": 0 },&nbsp; &nbsp; { "S1": 2, "C1": 7, "S2": 2, "C2": 5, "S3": 4, "C3": 9, "S4": 4, "C4": 6, "S5": 1, "C5": 9, "CLASS": 1 },&nbsp; &nbsp; { "S1": 2, "C1": 2, "S2": 1, "C2": 13, "S3": 2, "C3": 13, "S4": 4, "C4": 3, "S5": 4, "C5": 13, "CLASS": 3 },&nbsp; &nbsp; { "S1": 4, "C1": 10, "S2": 4, "C2": 11, "S3": 1, "C3": 10, "S4": 2, "C4": 9, "S5": 4, "C5": 1, "CLASS": 1 },&nbsp; &nbsp; { "S1": 4, "C1": 2, "S2": 3, "C2": 10, "S3": 4, "C3": 7, "S4": 4, "C4": 10, "S5": 2, "C5": 6, "CLASS": 1 },&nbsp; &nbsp; { "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) {&nbsp; &nbsp; let arr = [];&nbsp; &nbsp; for (let i = 0; i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i == val - 1 || (i == 0 && val == 0)) arr.push(1);&nbsp; &nbsp; &nbsp; &nbsp; else arr.push(0);&nbsp; &nbsp; }&nbsp; &nbsp; return arr;}function convertData(data) {&nbsp; &nbsp; const convertedData = data.map(item => {&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S1: arrayMaker(4, item.S1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C1: arrayMaker(13, item.C1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S2: arrayMaker(4, item.S2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2: arrayMaker(13, item.C2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S3: arrayMaker(4, item.S3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C3: arrayMaker(13, item.C3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S4: arrayMaker(4, item.S4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C4: arrayMaker(13, item.C4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S5: arrayMaker(4, item.S5),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C5: arrayMaker(13, item.C5),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLASS: arrayMaker(10, item.CLASS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; });&nbsp; &nbsp; return convertedData;}const convertedData = convertData(data);console.log(convertedData[0].CLASS);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答