数组中的对象值未定义

时间:2019-05-11 11:10:05

标签: javascript arrays object

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

所以我创建了对象数组。示例:

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

数据示例:

[
    { "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 }
]

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您没有创建对象数组。您正在.map(...)中创建对象数组。

map()使用返回的元素创建一个新数组,您将在地图中返回以下内容:

return [ // Returning an array.
    {...}
];

所以最终的数组将是这样:

[
    [ // The array returned in map
        {S1: ..., CLASS: ...}
    ],
    [ // The array returned in map
        {S1: ..., CLASS: ...}
    ]
]

请注意每个数组元素不是对象,而是包含对象的另一个数组。

您必须将.map()更改为此:

function convertData(data) {
    const convertedData = data.map(item => {
        return { // Note how I removed the [
            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)
        };  // Note how I removed the ]
    });
    return convertedData;
}

请注意如何删除[],而我只是返回想要的对象。

答案 2 :(得分:0)

您的代码似乎将数据包装在一个额外的数组中,因此将定义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);