Map method use instead of for loop - javascript

Trying to change my for loop with .map method of jquery.
But I am not getting the output which I use to get in for-loop.
This is my actual loop that get the perfect data in the form of array.
var yourArray = [];
for (var i = 0; i < marray.length; i++) {
mId = marray[i].id;
yourArray.push(mId);
}
same thing I tried using .map()
idArr = marray.map(function(row) {
return row[i].id;
});
But the actual array output is not coming.

It should be :
idArr = marray.map(function(row) {
return row.id;
});
row is the current value, not the entire array. So no need for the i.
Fiddle
Array.prototype.map()

Try using $.map() from jquery,
idArr = $.map(marray, function(v,i) {
return v.id;
});

Related

Get index from jquery array objects

I get this array in my chrome console, using this method $("#gallery_thumbnails .owl-item.active").get();:
Array[5]
0:div.owl-item.active
1:div.owl-item.active.synced
2:div.owl-item.active
3:div.owl-item.active
4:div.owl-item.active
But I want only the array indexs, like this:
How can I get this result?
Iterate over your object array and save keys to new array.
var out = [];
for (key in arrays) {
out.push(key);
}
console.log(out);
Or as suggested by other user, use this method:
var out = [];
for (var i = 0; i < array.length; i++) {
out.push(i);
}
console.log(out);
You could use the map method from jQuery
$.map($("#gallery_thumbnails .owl-item.active").get(), function (val, i) {
return i;
});

Looping through items in an array of objects

I have an array of objects.
ABC.getAggregation("V")[0].getItems();
this produces the result:
MY ARRAY OF OBJECTS
In the console i can get the result i am looking for by specifying the position of the item like this:
ABC.getAggregation("V")[0].getItems()[0].getPosition()
ABC.getAggregation("V")[0].getItems()[1].getPosition()
ABC.getAggregation("V")[0].getItems()[2].getPosition()
The result of the above code produces string values e.g "3.4554,43,0".
How can i loop through each item and get the position in my code. just like the above code that i typed in the console. there wont always be 3 objects this is why i cant hard code the above 3 lines.
Try using a the Array.prototype.forEach() function. The function will be called for each element in the array, passing in the element as the first parameter.
ABC.getAggregation("V")[0].getItems().forEach( function (item) {
item.getPosition();
//do something else
});
More on ".forEach()"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
You can treat it like any other array:
var myArray = ABC.getAggregation("V")[0].getItems();
for(var i=0; i< myArray.length; i++){
myArray[i].getPosition(); //Do something with the position.
}
You can use for loop to iterate trough all of them.
for(var i=0; i<ABC.getAggregation("V").getItems().length; i++) {
ABC.getAggregation("V")[0].getItems()[i].getPosition();
}
You can use forEach loop to iterate trough all of them.
ABC.getAggregation("V").getItems().forEach (item, index) {
return ABC.getAggregation("V")[0].getItems()[index].getPosition();
}
A very simple way to iterate through each object in the array is just with a for loop on the array, you don't even need to declare your iterating variable.
ex:
var anArray = ['one', 'two', 'three'];
for( i in anArray){
console.log('index #: ' + i );
console.log(anArray[i]);
}
will print out all the elements in anArray:
index #: 0
one
index #: 1
two
index #: 2
three
!! Apparently this is a good example of how not to do it :P
You can assign the items to an array and loop through them like this:
var items = ABC.getAggregation("V")[0].getItems();
var returnString = "";
for (var key in items ) {
if (items .hasOwnProperty(key)) {
var element = items [key];
returnString += element.getPosition() + ',';
}
}
returnString = returnString.substring(0, x.length-1);
console.log(returnString);

How do you push a specified number of objects to an array?

I'm pretty new to javascript but I'm trying to push a specified number of objects to an array using the following code. When I check the console I see only one object is pushed to the array. What should I be doing differently? Thanks!
var albums = {};
function collection(numberOfAlbums) {
array = [];
array.push(albums);
return array;
};
console.log(collection(12));
From your code:
array.push(albums);
would add the same object each time (assuming you had added a loop) which isn't what you want.
This will add a new empty object for each iteration of numberOfAlbums:
function collection(numberOfAlbums) {
for (var array = [], i = 0; i < numberOfAlbums; i++) {
array.push({});
}
return array;
};
Here's another way using map. Array.apply trick from here.
function collection(numberOfAlbums) {
var arr = Array.apply(null, Array(numberOfAlbums));
return arr.map(function (el) { return {}; });
};
I could give you the code but that is not learning. So here are the steps:
use numberOfAlbums as an argument in a function.
create an empty array.
use numberOfAlbums in for-loops in that for-loops push albums. ==array.push(albums)== do not use {}curly brackets around albums.
return the array.

Trying to convert a javascript object to array

I have a decoded Json variable called objIntChart that looks like the following when I console.log() it.
The problem is I need it as an array that looks like this
I did a foreach loop like this in an attempt to solve it:
var array = [];
objIntChart.forEach(function (entry) {
var x = 0++;
array.push(x);
array.x.push(entry['dateTime']);
array.x.push(entry['entries']);
});
However the problem is that I cannot do a push on array.x because it takes the x as the name and not the variable. Is there a solution to this?
Keep it simple:
var array = [];
objIntChart.forEach(function (entry) {
array.push([entry['dateTime'], entry['entries']]);
});
This will do as well:
var arr = objIntChart.map(function (obj) { return [obj.dateTimes, obj.entries]});
console.log(arr);
You should use array[x] instead of array.x and also be doing array[x] = [] or array.push([]) instead of array.push(x) which will add the integer x, not an empty array, to your array.
Also, you will need to set var x = 0; outside your forloop and do x++ inside it. Currently, x will always be 1 when you use it.

chaining methods and selectors using jquery inside of a loop

Is it possible to generate a chain of selectors and methods inside of a loop?
For example, I have an array of elements:
array[0] = '.type1value1, .type1value2, .type1value3';
array[1] = '.type2value1, .type2value2, .type2value3';
array[2] = '.type3value1, .type3value2, .type3value3';
I somehow need to build a chain of methods using the array elements as selectors (by looping or any other possible means!) so that I would end up with the following:-
$('.type1value1, .type1value2, .type1value3').filter('.type2value1, .type2.value2, .type2value3').filter('.type3value1, .type3value2, .type3value3');
If I understand you correctly, you don't even need to do a loop:
var firstSelector = array.shift(); //returns first item in the array and removes it from the original array
var filterSelector = array.join(',');
$(firstSelector).filter(filterSelector);
Why can't you just do something like:
var $test = $(array[0]);
for (var i = 1; i < array.length; i++) {
$test = $test.filter(array[i]);
}
Looking at your example, the value of each array element is exactly the value you want to pass as the selector parameter to .filter()
use the following function to pass it an array of selectors...
function getSet(arrSet){
var elements = $(arrSet[0]);
for (var i = 1; i < arrSet.length; i++) {
elements = $(elements).filter(arrSet[i]);
}
}

Categories

Resources