- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在 JavaScript 中创建两个数组并尝试使用 concat
连接它们时关键字,结果数组总是空的(好吧,数组中应该插入的东西没有插入)。我无法想象这实际上是 JS 应该如何工作的,因为那么...... concat
的意义是什么?关键字如果什么都不做?
那么我一定做错了什么,但我完全遵循文档。
这是一个演示我的问题的 fiddle :https://jsfiddle.net/webwhizjim/7z4v33of/
// Example with objects- doesn't work...
var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"},{"wowz":"wowzers!"}];
var obArray = [];
obArray.concat(greetings);
console.log("The jumble array is: " + obArray);
// Example with strings- still doesn't work...
var greetings2 = ["affirmative","yeah"];
var exclamations2 = ["omg"];
var obArray2 = ["huh?"];
[].concat.call(obArray2, greetings2);
console.log("The jumble array is: " + obArray2);
为了明确“它不起作用”,我的意思是控制台输出是这样的:
PS。在我的实际项目中,我使用的是 Angular 1.4;所以,如果有办法concat
数组,我对此持开放态度。
最佳答案
.concat()
创建一个新数组并返回它。它不会将元素添加到现有数组中。
来自MDN :
concat creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).
concat does not alter this or any of the arrays provided as arguments but instead returns a shallow copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:
您可以使用 .splice()
或 .push()
将元素添加到现有数组中。
var greetings2 = ["affirmative","yeah"];
var obArray2 = ["huh?"];
obArray2.push.apply(obArray2, greetings2);
// display result in snippet
document.write(JSON.stringify(obArray2));
或者,只需使用 .concat()
新返回的数组:
var greetings2 = ["affirmative","yeah"];
var obArray2 = ["huh?"];
var newArray = obArray2.concat(greetings2);
// display result in snippet
document.write(JSON.stringify(newArray));
关于javascript - 为什么数组串联在 Javascript 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35779096/
我正在尝试编写一行简单的代码,用于从各种服务器的 C 盘中删除各种用户文件。如何与 PowerShell 连接以获取到服务器的路径? 例如,这就是我想要做的,但 PowerShell 没有将 + 符号
我一直在尝试合并(连接)两个具有相同高度和宽度的 mp4 视频,但出现了一些错误。 代码是: FFmpeg ffmpeg = FFmpeg.getInstance(EditVideoActivity.
我正在尝试在 PostgreSQL 中进行简单的串联,但它不断抛出一条错误消息。我不明白我在这里做错了什么。 select concat('abcde', 'fgh'); No function ma
我不知道如何从一个函数中获取两个变量,这是我的菜鸟尝试,这可能吗?如果可以,它是如何完成的? #include float seriesParallel (float r1, float r2);/
我有以下代码: char stringHour[50], stringMinute[50], stringSecond[50]; // lots of code... itoa(hour, strin
我是套接字编程的新手,我正在尝试使用 TCP 实现客户端服务器。客户端在 Windows 中用 Java 编写,服务器在 Tandem/Hp-NonStop 中用 C 编写。我能够连接并向服务器发送请
到目前为止,我有以下内容。 DECLARE @Table Table (ID int, Value1 varchar(50), Value2 varchar(50), Value3 varchar(5
我是 Elasticsearch 的新手,想解决以下问题的用例 我有一些格式如下的文件 { "member_id":"number" "e_id":"number", "c_id":"numbe
我有这个功能来懒惰地观看日志文件... follow :: Handle -> IO [String] follow h = unsafeInterleaveIO $ do catch (do l
这个问题已经有答案了: List of lists changes reflected across sublists unexpectedly (17 个回答) 已关闭 3 年前。 我想创建一个没有
我们有一个 XML 文件,我们正试图找出一种动态使用的方法。 基本知识是这样的: 100.00 110.00 120.00 130.00 对于我们拥有的每个零件,我们希望在其上使用 xslt 以
我有一个很奇怪的问题。 下面的所有代码都工作正常: payload.modules.forEach(function(mod) { var modules_path = '..
如何使用 C 预处理自动生成以下代码模式(访问器)? // Immutable accessor. const auto& member1 () const { return _member1;
我有时间值来自数据库:00:30:00 这是我的代码: var gdtesttime = ; // 00:30:00 console.log(gdtesttime); var a = gdt
数据库:MySQL 我有两张表,一张用于用户分配的角色,一张包含角色信息。我的问题是分配的角色存储在单个字段中,用逗号分隔。我需要构建一个按名称而非 ID 列出角色的报告,但仍位于由列分隔的单个字段中
我在 ffmpeg 中使用以下命令,它会在视频开头添加 1 秒的黑帧。但是,我在输出视频中丢失了原始视频中的音频。我如何调整命令以确保原始音频与最终输出保持一致,或者更好的是,开头有 1 秒的“空白”
表 A 有 X 列,它是由表 B 中的 Y 列和 Z 列(均为 float )串联而成的 int。我想以类似于此的方式连接表 A 和 B: select * from tableA a inner j
pd.Series([1,1,1,1, "something", 1]).astype(float).cumsum().fillna(None) 我想在上面的系列中用 None 填充 NaN,但它告诉
在创建显示 OpenFileDialog/SaveFileDialog 的服务时,我正在考虑创建 LINQ 查询/清除 C# 代码到 Concatinate()/Join() 过滤表达式。 根据这个调
我正在尝试从数组中提取值并将所有值连接到一个字符串中,如下所示 var ingredient: String for tag in tags { if let text = tag.titleL
我是一名优秀的程序员,十分优秀!