为什么将元素推送到 concat( ) 返回的新数组中会返回数组的大小而不是数组本身
Why does pushing an element into a new Array returned by a concat( ) return the size of the array instead of the array itself?
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var a = ['a','b'];
var b = ['c','d'];
var c = a.concat(b).push('e');
document.getElementById("demo").innerHTML = c;
</script>
</body>
</html>
这将导致数字"5",而不是["a","b","c","d","e"]
根据定义,push() 方法返回调用该方法的对象的 new length
属性。
方法所基于的对象的新长度属性 叫。
这里
a.concat(b) //returns an `array`. But wait, the statement still has a method chained,
//and to be evaluated.
(returned array).push('e'); // the chained push() is invoked on the returned array.
这反过来又返回新形成的数组的length
。因此,语句的最终返回值是数组的length
,存储在 c
变量中。
若要通过concat()
操作捕获返回的array
,可以修改代码以将链接的方法分解为多个语句,如下所示:
var c = a.concat(b);
c.push('e');
console.log(c) // prints the array content.
相关文章:
- 对象数组返回自最近日期以来的最高总体值
- 从多维数组返回嵌套数组
- 为什么当我点击<a>使用纯javascript;这个“;返回url,数组返回“url”;未定义”;
- 带多维数组返回字符串的json_encode;阵列”;而不是数据
- 如何将 AJAX 响应作为 JavaScript 数组返回
- 我正在尝试在 JS 中制作这个随机名称生成器.数组返回未定义
- .push() 多个对象进入 JavaScript 数组返回 'undefined'
- 如何按属性从可观察数组返回项
- 通过jquery发送的关联php数组返回[对象对象]
- 从 Javascript 中的对象数组返回单个属性的数组
- 循环遍历对象数组返回“未定义”
- 拼接数组返回空的序列
- Javascript 多维数组返回第一个值,然后失败
- Json 数组返回空值
- 从下划线.js中的数组返回一系列值
- for 语句不从数组返回值
- MongoDB从数组返回特定字段
- 试图将数组返回到表中的列表中
- Javascript中的二维数组返回未定义的数组
- 将函数数组作为布尔值数组返回