arrays interview questions
Top arrays frequently asked interview questions
I have a value 'Dog'
and an array ['Cat', 'Dog', 'Bird']
.
How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?
Source: (StackOverflow)
I have an array of integers
, which I'm using the .push()
method to add to.
Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(int);
I have to use core JavaScript - no frameworks are allowed.
Source: (StackOverflow)
What is the most concise and efficient way to find out if a JavaScript array contains an obj?
This is the only way I know to do it:
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
Is there a better and more concise way to accomplish this?
This is very closely related to Stack Overflow question Best way to find an item in a JavaScript Array? which addresses finding objects in an array using indexOf
.
Source: (StackOverflow)
Possible Duplicate:
How do I Sort a Multidimensional Array in PHP
How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
Source: (StackOverflow)
I have an array of JavaScript objects:
var objs = [
{ first_nom: 'Lazslo', last_nom: 'Jamf' },
{ first_nom: 'Pig', last_nom: 'Bodine' },
{ first_nom: 'Pirate', last_nom: 'Prentice' }
];
How can I sort them by the value of last_nom
in JavaScript?
I know about sort(a,b)
, but that only seems to work on strings and numbers. Do I need to add a toString method to my objects?
Source: (StackOverflow)
What is the difference between using the delete
operator on the array element as opposed to using the Array.splice
method? For example:
myArray = ['a', 'b', 'c', 'd'];
delete myArray[1];
// or
myArray.splice (1, 1);
Why even have the splice method if I can delete array elements like I can with objects?
Source: (StackOverflow)
I read 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": "4",
"city": "Bevery Hills",
"state": "CA",
"zip": "90210",
"price": "319250"
}, {
"h_id": "5",
"city": "New York",
"state": "NY",
"zip": "00010",
"price": "962500"
}
];
How do I create a function to sort the objects by the price
property in ascending and descending order using only JavaScript?
Source: (StackOverflow)
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?
Source: (StackOverflow)
In Java you can use a for()
loop to go through objects in an array like so:
String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
//Do something
}
Can you do the same in JavaScript?
Source: (StackOverflow)
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?
Source: (StackOverflow)
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. Then I can loop over it without fear of an error.
So how do I check if the variable is an array?
I've rounded up the various solutions below and created a jsperf test.
Source: (StackOverflow)
If I have a JavaScript associative array, say:
var myArray = new Object();
myArray["firstname"] = "Gareth";
myArray["lastname"] = "Simpson";
myArray["age"] = 21;
Is there a built-in or accepted best practice way to get the length of this array?
JavaScript does not have associative arrays -- it only has objects, which can be used as a notion of associative arrays.**
Source: (StackOverflow)
What is the best method for adding options to a select from a JSON object using jQuery?
I'm looking for something that I don't need a plugin to do, but would also be interested in the plugins that are out there.
This is what I did:
selectValues = { "1": "test 1", "2": "test 2" };
for (key in selectValues) {
if (typeof (selectValues[key] == 'string') {
$('#mySelect').append('<option value="' + key + '">' + selectValues[key] + '</option>');
}
}
A clean/simple solution:
This is a cleaned up and simplified version of matdumsa's:
$.each(selectValues, function(key, value) {
$('#mySelect')
.append($('<option>', { value : key })
.text(value));
});
Changes from matdumsa's: (1) removed the close tag for the option inside append() and (2) moved the properties/attributes into an map as the second parameter of append().
Source: (StackOverflow)