陣列與迴圈

增刪、排序、去重、map/filter/reduce 與遍歷。

33 / 33 題 · 依票數排序

5k

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i…

2008javascriptarraysalgorithmtime-complexityjavascript-objects·3 重點答案·3.7M 次瀏覽
4.4k

How can I insert an item into an array at a specific index?

I am looking for a JavaScript array insert method, in the style of: arr.insert(index, item) It should preferably be in jQuery, but any JavaScript implementation will do at this po…

2009javascriptarrays·3 重點答案·3.7M 次瀏覽
4.2k

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: 'Prentice…

2009javascriptarrayssortingcomparisonarray-of-dict·3 重點答案·3.5M 次瀏覽
4.1k

Checking if a key exists in a JavaScript object?

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?

2009javascriptarraysobject·3 重點答案·3.9M 次瀏覽
4.1k

How to loop through the items of an array in JavaScript?

In Java, you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can I d…

2010javascriptarraysloopsfor-loop·3 重點答案·5.6M 次瀏覽
3.6k

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; How do I loop through all of p 's elements ( p1 , p2 , p3 ...) and get t…

2009javascriptloopsfor-loopeach·3 重點答案·2.9M 次瀏覽
3.4k

How can I check if an object is an array?

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can l…

2011javascriptarraysjavascript-objects·3 重點答案·2.1M 次瀏覽
2.9k

How to append something to an array?

How do I append an object (such as a string or number) to an array in JavaScript?

2008javascriptarraysappend·3 重點答案·5.5M 次瀏覽
2.5k

How to create an array containing 1...N

I'm looking for any alternatives to the below for creating a JavaScript array containing 1 through to N where N is only known at runtime. var foo = []; for (var i = 1; i <= N; i++…

2010javascriptarraysrange·3 重點答案·2.9M 次瀏覽
2.4k

Iterate through object properties

var obj = { name: "Simon", age: "20", clothing: { style: "simple", hipster: false } } for(var propt in obj){ console.log(propt + ': ' + obj[propt]); } How does the variable propt…

2011javascriptloopsobject·3 重點答案·1.7M 次瀏覽
2.2k

Short circuit Array.forEach like calling break

[1,2,3].forEach(function(el) { if(el === 1) break; }); How can I do this using the new forEach method in JavaScript? I've tried return; , return false; and break . break crashes a…

2010javascriptarraysforeach·3 重點答案·1.9M 次瀏覽
2.2k

How do I empty an array in JavaScript?

Is there a way to empty an array and if so possibly with .remove() ? For instance, A = [1,2,3,4]; How can I empty that?

2009javascriptarrays·3 重點答案·4.4M 次瀏覽
2.2k

Get the last item in an array

Here is my JavaScript code so far: var linkElement = document.getElementById("BackButton"); var loc_array = document.location.href.split('/'); var newT = document.createTextNode(u…

2010javascriptarrays·3 重點答案·3.7M 次瀏覽
2.2k

How to create a copy of a JavaScript array instead of a reference?

When copying an array in JavaScript to another array: var arr1 = ['a','b','c']; var arr2 = arr1; arr2.push('d'); console.log(arr1); //= ['a','b','c','d'] I realized that arr2 refe…

2011javascriptarraysdeep-copypass-by-value·3 重點答案·1.3M 次瀏覽
2k

How to merge two arrays in JavaScript and de-duplicate items

I have two JavaScript arrays: var array1 = ["Vijendra","Singh"]; var array2 = ["Singh", "Shakya"]; I want the output to be: var array3 = ["Vijendra","Singh","Shakya"]; The output…

2009javascriptarraysmerge·3 重點答案·2.3M 次瀏覽
1.7k

Sorting an array of objects by property values

I've got the following objects using AJAX and stored them in an array: var homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", "price": "162500" }, { "h_id":…

2009javascriptarrayssorting·3 重點答案·1.6M 次瀏覽
1.7k

Remove empty elements from an array in Javascript

How do I remove empty elements from an array in JavaScript? Is there a straightforward way, or do I need to loop through it and remove them manually?

2008javascriptarrays·3 重點答案·1.9M 次瀏覽
1.6k

Merge/flatten an array of arrays

I have a JavaScript array like: [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]] How would I go about merging the separate inner arrays into one like: ["$6", "$12",…

2012javascriptarraysmultidimensional-arraymergeflatten·3 重點答案·1.3M 次瀏覽
1.6k

How to compare arrays in JavaScript?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to…

2011javascriptarraysjson·3 重點答案·1.9M 次瀏覽