Javascript JS 嵌套数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3984735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS nested arrays
提问by Diesal11
Hey, Im trying to make a nested array in JS
嘿,我想在 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's debugger tells me the ), at the end of the first nested array is an "Unexpected Token"
Chrome 的调试器告诉我 ),在第一个嵌套数组的末尾是一个“意外令牌”
回答by Jordan Running
From your code it looks like you're trying to use PHP-style arrays in JavaScript. JavaScript arrays don't work like PHP arrays. Here's something that's more likely to work:
从您的代码来看,您似乎正在尝试在 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',
},
];
To explain a littler further, in JavaScript you create a new array like this:
进一步解释一下,在 JavaScript 中,您创建一个新数组,如下所示:
const myArray = [ 5, 10, 15 ];
The square brackets ([]
) denote the beginning and end of the array and commas (,
) separate the elements of the array. Then, to access the elements, we would do something like this:
方括号 ( []
) 表示数组的开头和结尾,逗号 ( ,
) 分隔数组的元素。然后,要访问元素,我们将执行以下操作:
alert( myArray[0] );
...which would give 5
(the first, or "0th," element in the array).
...这将给出5
(数组中的第一个或“第 0 个”元素)。
Now, whereas PHP has the associative array (array('a' => 1, ...)
), in JavaScript there's no "associative array"; rather, you use an "object literal," like this:
现在,虽然 PHP 有关联数组 ( array('a' => 1, ...)
),但在 JavaScript 中没有“关联数组”;相反,您使用“对象文字”,如下所示:
const myObject = { a: 5, b: 10, c: 15 };
This creates a new object with properties (you can think of them as keys) named a
, b
, and c
. There are two ways to access a property:
这将创建与属性的新对象(你可以把它们想象成键)命名的a
,b
和c
。访问属性有两种方式:
alert( myObject['b'] );
alert( myObject.b );
In both cases, 10
(the value we assigned to property b
) would be given.
在这两种情况下,10
(我们分配给 property 的值b
)都会被给出。
Now back to your exercise. You'll see that here we've created an array ([]
) and given it three elements, each of which is an object literal ({}
). To access, say, the user
property of the first element, we would do this:
现在回到你的锻炼。您会看到,我们在这里创建了一个数组 ( []
) 并为其指定了三个元素,每个元素都是一个对象字面量 ( {}
)。例如,要访问user
第一个元素的属性,我们将执行以下操作:
alert( lines[0].user ); // => "User1"
Edit:If you want to name the elements of the outer array, it must be changed to an object literal, and can be nested like so:
编辑:如果要命名外部数组的元素,则必须将其更改为对象文字,并且可以像这样嵌套:
const lines = {
one: {
time: '10:00:00',
user: 'User1',
content: 'Line1',
},
two: {
// ...
},
// ...
};
I've named the items one
, two
, etc. for clarity's sake, but you can use any values you please. However, if you intend to use numeric property names—0
, 1
, 2
, etc—as in your example code, you may as well use the other array rather than the object. Unlike PHP, JavaScript does not allow "gaps" in an array—they'll be filled with undefined
. For example:
我命名的项目one
,two
等,为清晰起见,但你可以使用任何值,您请。但是,如果您打算在示例代码中使用数字属性名称 - 0
、1
、2
等,则最好使用另一个数组而不是对象。与 PHP 不同,JavaScript 不允许数组中存在“间隙”——它们将被undefined
. 例如:
const myArr = [1, 2];
myArr[5] = 3;
alert( myArr ); // => [ 1, 2, undefined, undefined, undefined, 3 ];
回答by tsurahman
try this
尝试这个
var lines = [ {'time': 'the time', 'user': 'the user', 'content': 'the content'}, {'time': 'the time', 'user': 'the user', 'content': 'the content'}];
回答by Shadab Ali
here is simple example that will solve tell you about array in side array in java script if element is even it replace with string "even" for odd replace with "odd"
这是一个简单的例子,它会告诉你java脚本中侧数组中的数组,如果元素是偶数,它替换为字符串“偶数”,奇数替换为“奇数”
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);
回答by Aathi
Simple way to add all nested values
添加所有嵌套值的简单方法
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
回答by Aathi
a small, standard, 'eye' fix
一个小的、标准的、“眼睛”修复
var lines = [{
'time': 'the time',
'user': 'the user',
'content': 'the content'
},
{
'time': 'the time',
'user': 'the user',
'content': 'the content'
}];
回答by srishti sharma
well,i don't think the problem is with the square brackets because in javaScript arrays can be declared in any of the following ways -
好吧,我不认为问题出在方括号上,因为在 javaScript 数组中可以通过以下任何一种方式声明 -
var arr = new Array(element0, element1, ..., elementN);
var arr = Array(element0, element1, ..., elementN);
var arr = [element0, element1, ..., elementN];
For more solid reference visit this MDN link.
The problem according to me could be the choice of keys.
P.S. i am a beginner and just learning from here and there.If i got it wrong then please correct me because this SO question has dragged me into confusion.
如需更可靠的参考,请访问此 MDN链接。在我看来,问题可能是键的选择。
PS我是一个初学者,只是从这里和那里学习。如果我弄错了,请纠正我,因为这个问题让我陷入了混乱。