从数组填充另一个数组 - Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3438027/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:37:09  来源:igfitidea点击:

Populating another array from array - Javascript

javascript

提问by ina

Very simple thing I am trying to do in JS (assign the values of one array to another), but somehow the array bar's value doesn't seem affected at all.

我试图在 JS 中做的非常简单的事情(将一个数组的值分配给另一个),但不知何故,数组bar的值似乎根本不受影响。

The first thing I tried, of course, was simply bar = ar;-- didn't work, so I tried manually looping through... still doesn't work.

当然,我尝试的第一件事很简单bar = ar;——没有用,所以我尝试手动循环……仍然不起作用。

I don't grok the quirks of Javascript! Please help!!

我不理解 Javascript 的怪癖!请帮忙!!


var ar=["apple","banana","canaple"];
var bar;

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);

And, here is the fiddle: http://jsfiddle.net/vGycZ/

而且,这是小提琴:http: //jsfiddle.net/vGycZ/



(The above is a simplification. The actual array is multidimensional.)

(以上是简化。实际数组是多维的。)

回答by CMS

Your code isn't working because you are not initializing bar:

您的代码不起作用,因为您没有初始化bar

var bar = [];

You also forgot to declare your ivariable, which can be problematic, for example if the code is inside a function, iwill end up being a global variable (always use var:).

您还忘记声明您的i变量,这可能会出现问题,例如,如果代码在函数内部,i最终将成为全局变量(始终使用var:)。

But, you can avoid the loop, simply by using the slicemethod to create a copy of your first array:

但是,您可以避免循环,只需使用该slice方法创建第一个数组的副本:

var arr = ["apple","banana","canaple"];
var bar = arr.slice();

回答by Pranay Rana

copy-or-clone-javascript-array-object

复制或克隆javascript-array-object

var a = [ 'apple', 'orange', 'grape' ];
 b = a.slice(0);

回答by Luis Cabrera Benito

In ES6 you can use Array.from:

在 ES6 中,您可以使用Array.from

var ar = ["apple","banana","canaple"];
var bar = Array.from(ar);
alert(bar[1]); // alerts 'banana'

回答by Daniel Vassallo

You have two problems:

你有两个问题:

First you need to initialize baras an array:

首先你需要初始化bar为一个数组:

var bar = [];

Then arrshould be arin here:

那么arr应该ar在这里:

for(i=0;i<arr.length;i++){
for(i=0;i<arr.length;i++){

Then you'll get alerted your banana :)

然后你会收到通知你的香蕉:)

回答by Arghya C

With ES6+ you can simply do this

使用ES6+ 你可以简单地做到这一点

const original = [1, 2, 3];
const newArray = [...original];

The documentation for spread syntax is here

传播语法的文档在这里

To check, run this small code on dev console

要检查,请运行此小代码 dev console

const k = [1, 2];
const l = k
k === l
> true
const m = [...k]
m
>?[1, 2]
k === m
> false

回答by Codler

You have misspelled variable ar Try this

你拼错了变量 ar 试试这个

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);

回答by Artem Barger

The problem probably here in the for loop statement:

问题可能出现在 for 循环语句中:

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);

You need to fix to ar.lengthinstead of arr.length.And it's better to initialize bar as: var bar = [].

您需要修复 toar.length而不是arr.length.And 最好将 bar 初始化为:var bar = []

回答by slebetman

In your code, the variable arrin the for loop is not the same as your original array ar: you have one too many r.

在您的代码中,arrfor 循环中的变量与您的原始数组不同ar:您有一个太多的r.