JS嵌套数组

时间:2010-10-21 06:11:00

标签: javascript arrays

嘿 我试图在JS中创建一个嵌套数组

    var lines = new Array(
                    "0"= new Array(
                                0['time']="10:00:00",
                                0['user']="User1",
                                0['content']="Line1",
                                ),
                    "1"= new Array(
                                1['time']="20:00:00",
                                1['user']="User2",
                                1['content']="Line2",
                                ),
                    "2"= new Array(
                                2['time']="30:00:00",
                                2['user']="User3",
                                2['content']="Line3",
                                ),
                    );

Chrome的调试器告诉我),在第一个嵌套数组的末尾是“意外的令牌”

6 个答案:

答案 0 :(得分:30)

从您的代码中看起来您正试图在JavaScript中使用PHP样式的数组。 JavaScript数组不像PHP数组那样工作。这是更有可能发挥作用的东西:

const lines = [
  { time: '10:00:00',
    user: 'User1',
    content: 'Line1',
  },
  { time: '20:00:00',
    user: 'User2',
    content: 'Line3',
  },
  { time: '30:00:00',
    user: 'User3',
    content: 'Line3',
  },
];

为了进一步解释一下,在JavaScript中你可以创建一个像这样的新数组:

const myArray = [ 5, 10, 15 ];

方括号([])表示数组的开头和结尾,逗号(,)分隔数组的元素。然后,要访问元素,我们会这样做:

alert( myArray[0] );

...这将给出5(数组中的第一个或“第0个”元素)。

现在,虽然PHP有关联数组(array('a' => 1, ...)),但在JavaScript中没有“关联数组”;相反,你使用“对象文字”,如下所示:

const myObject = { a: 5, b: 10, c: 15 };

这将创建一个新对象,其中包含名为abc的属性(您可以将其视为键)。有两种方法可以访问属性:

alert( myObject['b'] );
alert( myObject.b );

在这两种情况下,都会给出10(我们赋予属性b的值)。

现在回到练习中。你会看到我们在这里创建了一个数组([])并给它三个元素,每个元素都是一个对象文字({})。要访问第一个元素的user属性,我们会这样做:

alert( lines[0].user ); // => "User1"

编辑:如果要命名外部数组的元素,则必须将其更改为对象文字,并且可以嵌套,如下所示:

const lines = {
  one: {
    time: '10:00:00',
    user: 'User1',
    content: 'Line1',
  },
  two: {
    // ...
  },
  // ...
};

为了清楚起见,我已将项目命名为onetwo等,但您可以使用任何您喜欢的值。但是,如果您打算使用数字属性名称 - 012等 - 如示例代码中所示,您也可以使用其他数组而不是对象。与PHP不同,JavaScript不允许在数组中使用“间隙” - 它们将填充undefined。例如:

const myArr = [1, 2];
myArr[5] = 3;
alert( myArr ); // => [ 1, 2, undefined, undefined, undefined, 3 ];

答案 1 :(得分:3)

试试这个

var lines = [ {'time': 'the time', 'user': 'the user', 'content': 'the content'}, {'time': 'the time', 'user': 'the user', 'content': 'the content'}];

答案 2 :(得分:1)

这里有一个简单的例子,它将解决在java脚本中的侧数组中的数组,如果元素甚至用字符串替换#34;偶数"用奇数代替"奇数"

 var numbers = [
        [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
        [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
        [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
        [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
        [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
        [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
        [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
        [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
        [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
        [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
    ];

    for(i=0; i < numbers.length ; i++ ) 
    {
    for(j=0 ; j < numbers[i].length; j++)
    {
        if(numbers[i][j]%2===0)
        {
            numbers[i][j]="even";
        }
        else
        numbers[i][j]="odd";


    }

    }
    console.log(numbers);

答案 3 :(得分:0)

一个小的标准“眼睛”修复


var lines = [{
    'time': 'the time',
    'user': 'the user',
    'content': 'the content'
},
{
    'time': 'the time',
    'user': 'the user',
    'content': 'the content'
}];

答案 4 :(得分:0)

好吧,我认为问题不在于方括号,因为在javaScript数组中可以用以下任何一种方式声明 -

var arr = new Array(element0, element1, ..., elementN);
var arr = Array(element0, element1, ..., elementN);
var arr = [element0, element1, ..., elementN];

要获得更加可靠的参考,请访问此MDN link。 根据我的问题可能是钥匙的选择 附:我是一个初学者,只是从这里和那里学习。如果我弄错了,那么请纠正我,因为这个问题让我陷入困惑。

答案 5 :(得分:0)

添加所有嵌套值的简单方法

const order = [
    {
        "quantity": 1,
        "id": "3833085c-864b-47d7-b2a8-e37ef60d3691",
        "val": {
            "sellable": true,
            "out_of_stock": false,
            "discount": null,
            "pack_size": "50",
            "productPrice": 29.5,
            "unit_price": 0.59,
            "name": "Butter Chilli",
            "tamil_name": null,
            "id": "3833085c-864b-47d7-b2a8-e37ef60d3691",
            "price": 29.5,
            "wholesale_price": 29.5,
            "stock": 100,
            "cost_price": null,
            "unit_weight": "50g"
        }
    },
    {
        "quantity": 2,
        "id": "75fe08ab-8438-42db-bb26-da77568b1fea",
        "val": {
            "sellable": true,
            "out_of_stock": false,
            "discount": null,
            "pack_size": "15",
            "productPrice": 29.25,
            "unit_price": 1.95,
            "name": "Anchovy (Nethaly) H/O(Small)",
            "id": "75fe08ab-8438-42db-bb26-da77568b1fea",
            "price": 29.25,
            "wholesale_price": 29.25,
            "stock": 100,
            "unit_weight": "700gm"
        }
    }
]

const totalAmount = (order) => {
    return order.map(product => product.val.price * product.quantity ).reduce((a, b) => a + b, 0).toFixed(2)
}

// Total value is 
console.log(totalAmount(order))
88.00