How to access a element in JavaScript array? - javascript

I have a JS array:
a = ["a",["b","c"]]
How I can access string "b" in this array? Thank you very much!

You index into an array like this:
a[1][0]
Arrays are accessed using their integer indexes. Since this is an array inside an array, you use [1] to access the inner array, then get the first item in that array with [0].

That is a[1][0]
alert(a[1][0]) // "b"

As there is an alternative way also to access the element in the array which is:
a['1']['0'] //"b"
as array is internally an object so think indexes is a property of that object so
a = ["a",["b","c"]]
can be internally and object keys or property are internally transfer to string so:
a = {
'0' : "a",
'1' : ["b", "c"]
}
this also can refactor to:
a = {
'0' : "a",
'1' : {
'0' : "b",
'1' : "c"
}
}
so we can access that index as:
a['1']['0']
this will give the value as b.

Related

array.filter is undefined when looping over objects

I have an array of objects where the value i'm trying to find is a number, i'm wanting to return the underlying objects array when the correct number is found, and the value i'm passing to search is type number.
I keep getting "array.filter is undefined" error. I'm assuming it's because the structure is one object and not an array? Whats the best way to do this?
I have a fiddle here
var obj = array.filter(function ( obj ) {
return obj === 2000;
})[0];
console.log( obj );
Your "array" is not an array - its an object
var array = {
"legend": {
....
}
}
What you want instead is just read properties of an object - some of them are numeric, which means you'll need the square bracket notation:
var obj = array.legend["2000"];
Updated fiddle: http://jsfiddle.net/gJPHw/221/

NodeJS Difference Object.keys(array).length and array.length

I have a function which reads Files from the file system and stores them into an array. Afterwards I want to add a key/value pair to that element. However, the forEach loop is not executed, because apparently there is no element in there.
readFilesFromDirectory(folder, elements, 'json', function(){
log(Object.keys(elements).length,0);
log(elements.length,0);
elements.forEach(function(elem){
elem["newKey"] = 1;
});
});
My log contains the following lines:
1
0
The first length method is working, the second is not.
I would like to know what I am doing wrong for the second function and how I can fix it.
Actually, my main objective is to add the new key. However, I do not know how to use some Object.keyValues(elements).forEach(function(elem){...} in my code. If you have a hint for that, this would also be nice.
I would really appreciate some insight here! :-)
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
Object.keys returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.
var arr = ["a", "b", "c"];
alert(Object.keys(arr)); // will alert "0,1,2"
// array like object
var obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // will alert "0,1,2"
// array like object with random key ordering
var an_obj = { 100: "a", 2: "b", 7: "c"};
alert(Object.keys(an_obj)); // will alert "2, 7, 100"
// getFoo is property which isn't enumerable
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } }});
my_obj.foo = 1;
alert(Object.keys(my_obj)); // will alert only foo

How to delete an object from array by reference (javascript)?

Simple question, but I cannot find a solution.
I have an array of objects.
I also have a reference to an object from this array.
I want to delete the object from the array.
How to do it in Javascript (without comparing object properties)?
PS It is easy to do it in C# (using List collection)
List<SomeObject> list = ........ ;
SomeObject element = ......... ;
list.Remove(element);
You can use indexOf to get the index of the object and splice to remove it from the array:
var arr = [ { name: 0}, { name : 1 } , {name : 2 } ];
var myObj = arr[1];
arr.splice(arr.indexOf(myObj),1);
console.log(arr);
There is no way to do this with arrays directly. You will have to find or roll your own implementation of an collection which supports similar operation.

Reach into a JavaScript array object

If I console log test
I get
[
{
property_1: "a",
property_2: "b",
}
]
How can I console log the value of property_1 ?
console.log(test[0].property_1);
test is an array, who's first element is a map with keys property_1, and property_2.
test[0] accesses the first element of the array, which is a map. From there you can directly access the properties with the dot notation, or with a string subscript:
console.log(test[0]["property_1"]);
console.log(test[0]["property_1"])
First go into the array:
my_arr[0]
Then to get the property:
my_arr[0]['property_1']
End result:
var my_arr = [
{
property_1: "a",
property_2: "b",
}
]
alert(my_arr[0]['property_1']);
If that's what you get when you console.log, then I'd bet that you have a JSON string that needs to be parsed.
If so, do this...
var parsed = JSON.parse(test);
alert(parsed[0].property_1);

How to get the first value in a javascript array

I want to get first file name (Apple_Desk_1920 x 1200 widescreen.jpg) in the array img declared below. How do I do this?
This is my code:
var img = [{
"image" : "Apple_Desk_1920 x 1200 widescreen.jpg"
}, {
"image" : "aa.jpg"
}, {
"image" : "auroracu4.jpg"
}, {
"image" : "blue-eyes-wallpapers_22314_1920x1200.jpg"
}, {
"image" : "blue-lights-wallpapers_22286_1920x1200.jpg"
}, {
"image" : "fuchsia-wallpapers_17143_1920x1200.jpg"
}, {
"image" : "leaves.jpg"
}, ];
It's:
var variableName = img[0].image;
What you have there is an array of objects. To get an array entry, you use [] with an array index (0 to one less than the length of the array). In this case, that gives you a reference to the object. To access an object's properties, you can use literal notation as I have above (obj.image), or using [] with a string property name (obj["image"]). They do exactly the same thing. (In fact, the [] notation for accessing object properties is what you're using when you "index" into an array; JavaScript arrays aren't really arrays, they're just objects with a couple of special features.)
So breaking the line above down:
var variableName = // Just so I had somewhere to put it
img[0] // Get the first entry from the array
.image; // Get the "image" property from it
// dot notation
console.log(img[0].image);
or:
// square-bracket notation
console.log(img[0]['image']);
will get it for you, since you have an array of objects.

Categories