我有一个Numbers数组,我正在使用该.push()方法向其中添加元素.
有没有一种简单的方法从数组中删除特定元素?相当于像array.remove(number);.
我必须使用核心的JavaScript - 无框架是不允许的.
我被告知不要for...in在JavaScript中使用数组.为什么不?
我有一系列数字,我需要确保它们是唯一的.我在互联网上找到了下面的代码片段,它的工作情况很好,直到数组中的数字为零.我发现这个其他脚本在SO上看起来几乎就像它,但它不会失败.
所以为了帮助我学习,有人可以帮我确定原型脚本出错的地方吗?
Array.prototype.getUnique = function() {
var o = {}, a = [], i, e;
for (i = 0; e = this[i]; i++) {o[e] = 1};
for (e in o) {a.push (e)};
return a;
}
Run Code Online (Sandbox Code Playgroud)
我很难搞清楚如何移动数组元素.例如,给出以下内容:
var arr = [ 'a', 'b', 'c', 'd', 'e'];
Run Code Online (Sandbox Code Playgroud)
我怎么能写一个'd'以前移动的函数'b'?
还是'a'之后'c'?
移动后,应更新其余元素的索引.这意味着在第一个例子中,移动arr [0]将='a',arr [1] ='d'arr [2] ='b',arr [3] ='c',arr [4] = 'E'
这看起来应该很简单,但我无法绕过它.
你如何从数组中获得第一个元素:
var ary = ['first', 'second', 'third', 'fourth', 'fifth'];
Run Code Online (Sandbox Code Playgroud)
我试过这个:
alert($(ary).first());
Run Code Online (Sandbox Code Playgroud)
但它会回来[object Object].
所以我需要从数组中获取应该是元素的第一个元素'first'.
我有这样一个数组:
arr = []
arr[0] = "ab"
arr[1] = "abcdefgh"
arr[2] = "abcd"
Run Code Online (Sandbox Code Playgroud)
排序后,输出数组应为:
arr[0] = "abcdefgh"
arr[1] = "abcd"
arr[2] = "ab"
Run Code Online (Sandbox Code Playgroud)
我的意思是,我希望按每个元素长度的降序排列.
我有这样的数组
arr = ["orange","red","black","white"]
Run Code Online (Sandbox Code Playgroud)
我想扩充定义一个deleteElem()方法的数组对象,其行为如下:
arr2 = arr.deleteElem("red"); // ["orange","black","white"] (with no hole)
Run Code Online (Sandbox Code Playgroud)
仅使用value参数(无索引)完成此任务的最佳方法是什么?
我有一个这样的数组:
var flightPlanCoordinates = [
{ lat: 37.772, lng: -122.214, status: "walking" },
{ lat: 36.772, lng: -123.214, status: "walking" },
{ lat: 21.291, lng: -157.821, status: "automotive" },
{ lat: -18.142, lng: 178.431, status: "automotive" },
{ lat: -27.467, lng: 153.027, status: "walking" },
{ lat: -26.467, lng: 151.027, status: "walking" },
];
Run Code Online (Sandbox Code Playgroud)
我希望将其拆分为三个数组,这些数组具有相同类型的对象,如下格式:
[{lat: 37.772, lng: -122.214, status: 'walking'},
{lat: 36.772, lng: -123.214, status: 'walking'}]
[{lat: 21.291, lng: -157.821, status: 'automotive'},
{lat: -18.142, lng: 178.431, status: 'automotive'}]
[{lat: …Run Code Online (Sandbox Code Playgroud) LINQ 有一个类似的问题:在 LINQ 中是否有等价物None()?
集合/数组有一些布尔方法:
Array.some(类似于linq.Any)Array.every(类似于linq.All)我可以检查数组中是否没有元素与给定的函数回调匹配
一个可能的解决方法是.filter然后检查.length并确保它为零:
let arr = ["a","b","c"]
// make sure that no item in array = "b"
let noBs = arr.filter(el => el === "b").length === 0
Run Code Online (Sandbox Code Playgroud) 请参考 - https://jsfiddle.net/53ranmn5/1
Array.prototype.method1 = function() {
console.log("method1 called");
}
[1,2,3,4].method1();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误,
类型错误:无法读取性能
'method1'的undefined
为什么这样?我怎样才能解决这个问题?
我正在尝试从头开始实现reduce()。我能够让它工作,但是即使我从未在任何地方定义过它,javascript如何知道“数组”是什么?
function reduce(callback, initialVal) {
var accumulator = (initialVal === undefined) ? undefined : initialVal;
for (var i=0; i<array.length; i++) {
if (accumulator !== undefined) {
accumulator = callback(accumulator, array[i], i, array);
} else {
accumulator = array[i]
}
}
return accumulator;
};
// testing a basic sum
arr = [1,2,3]
arr.reduce( (accumulator, elem) => accumulator+=elem )
Run Code Online (Sandbox Code Playgroud)
编辑:我开始工作了:DI 将“array”更改为“this”,因为我在 Array.prototype 下创建了一个新方法。
Array.prototype.myReduce = function(callback, initialVal) {
var accumulator = (initialVal !== undefined) ? initialVal : undefined;
for (var i=0; i<this.length; …Run Code Online (Sandbox Code Playgroud)