I have two ng-repeat i want to display them in the grid one by one alternately from both of the tr's. Like first index of first tr then first index of 2nd tr. If any help really appreciable. Thanks in advance.
Create a function to weave your two arrays together. Something like this:
function combine(arr1, arr2){
var result = [];
for(var i=0; i < arr1.length; i++){
result.push(arr1[i]);
result.push(arr2[i]);
}
return result;
}
(this is a simple example, and doesn't account for the arrays being different sizes.)
Then you can assign a $scope variable to the result of this function, and use that variable in the ng-repeat.
Demo
Create in the scope an array containing the stuff from your 2 initial arrays and then you'll be able to ng-repeat the big array containing all your elements in the right order.
Related
I'm trying to call a REST API using for loop and store the output to an array. But the output shows in different arrays for each and every rest API call instead of everything in one array
for each (name in car.cars){
for(i=0; i<count;i++){
var arr = [];
var newid = car.cars[i].name;
var url = "myhost"
var method = "GET"
var response = "output from REST call"
arr.push(response)
}
}
But the output shows in different arrays for each and every rest API call instead of everything in one array
Where is "the output" there's nothing in your code.
Your Problem is that you're declaring the var arr = []; inside your for loop.
Initialize the array before the loop starts and just add your responses to that array.
Instead your creating a new array for each iteration of for(i=0; i<count;i++)
Take a step back and look at your code again. Where are you declaring your array? Inside the loop.
Next, ask yourself; what is the scope of that array? Only within the loop. Even if just in the outer loop, it won't stick around because as soon as that loop finishes, the array disappears.
Create the array and use it from outside the loops (both of them).
Further reading: Declaring variables inside or outside of a loop
UPDATE 4/30/2019: Thanks to #AuxTaco, I crossed out my inaccurate description of scope in JS. Please see the links in his comment for more reading on this!
I'm getting two extra/unexpected array entries that are undefined. These show up when I iterate over an array of element nodes with 3 entries and push the values into another array:
I have a select element with 4 options in it. I want to put the last 3 options.innerText into an array
<select>
<option>skip me</option>
<option>text1</option>
<option>text2</option>
<option>text3</option>
<select>
I made a variable and grabbed the nodes:
var options = document.querySelectorAll('select option:not(:first-child)')
this gave me an array with 3 option elements in it as confirmed in the console
>>options
<<[<option>text1</option>,
<option>text2</option>,
<option>text3</option>]
options.length is 3.
iterating over the array proceeds as expected, but also includes a function in the log?
>>for (i in options){console.log(options[i])}
<< <option>text1</option>
<option>text2</option>
<option>text3</option>
<< 3
<< function item() {[native code]}
When I iterate over the array and push the innerText into a new array, I get not 3, but 5 entries in the array
>>var texts = [];
>>for (i in options){texts.push(options[i].innerText)}
This gives me an array texts with 5 values: ['text1','text2','text3',undefined,undefined]
texts.length is 5
Why am I getting those two extra array entries?
I'm sure this is something elementary that I just haven't come across yet, can anyone explain?
Try iterating through a general for loop.
for (var i = 0; i < options.length; i++) {
texts.push(options[i].innerText)
}
http://jsfiddle.net/d02urj1n/
Because for (i in options) iterates through the properties of the options object. for/in was made to iterate over enumerable properties.
Array indexes are just enumerable properties with integer names and
are otherwise identical to general Object properties. There is no
guarantee that for...in will return the indexes in any particular
order and it will return all enumerable properties, including those
with non–integer names and those that are inherited.
So, while you may find some iterations of forEach or you can roll out your own, it is safer to loop over an array using a normal for loop.
If you are using jquery use $.map() like so...
var opts = $("select > option:not(:first-child)");
var texts = [];
$.map(opts, function(val, key){
texts.push(val.innerText);
});
I suggest to use basic JavaScript For Loop, just like following :
for (var i=0 ; i < options.length ; i++){
console.log(options[i]);
}
//That will give you the expected result
<option>text1</option>
<option>text2</option>
<option>text3</option>
NOTE : Please take a look at Why is using “for…in” with array iteration such a bad idea?.
Hope this helps.
Looping through the properties in the object gives you the following items:
"0": <option>text1</option>
"1": <option>text2</option>
"2": <option>text3</option>
"length": 3
"item": function () {[native code]}
The reason that you get a length and item property also, is that the object is not an array, it's a NodeList object. It works as an array for methods that expect an array because it has a length property and numbered items, so it's what's called an array-like object. The for( in ) loop doesn't expect an array.
Loop through the items as if it was an array, using the length property:
var texts = [];
for (var i = 0; i < options.length; i++){
texts.push(options[i].innerText);
}
Thanks everyone, after some more inspection I did find some injected code objects! The standard for loop worked as expected. I didn't realize that for... in... would bring in inherited properties. Learn something new everyday.
Ok so essentially i have what i think is a JSON object. You see its properties in the picture i provided, now pretty much what i have been trying to do is to write a for each for a particular lvl.
$.each(toSort.items.items.items.items.items, function (index, value) {
console.log(index);
});
So pretty much what i want is a loop nested in the 5th layer, run code. so what i want to know is why is the code above invalid?
Because items is always an array you would have to refer to a certain index within this array. If you want to get one single item you must use the indexes, too.
toSort[0].items[0].items[0] //third level
If you want all values from that arrays you are better off using more than one loop. Moreover for() is much faster than jQuery's each().
for(var i = 0; i < toSort i++){
//first level
for(var j=0; j < toSort[i].items; j++){
//second level
for(var x=0; x < toSort[i].items[j].items; x++){
//third level
}
}
}
Items are arrays in every layer before 5th as well, so to access the items array inside the first layer you need to specify an index, by doing toSort.items.items, the second items is beign accessed as a property, that doesn't exist, to access the second items array inside the first items array you must access it as toSort.items[0].items and so on.
An example of subsequent access might be
toSort.items[0].items[0].items[0].items[0].items[0]
toSort.items[0].items[0].items[0].items[0].items[1]
toSort.items[0].items[0].items[0].items[0].items[2]
...
toSort.items[0].items[0].items[0].items[1].items[0]
toSort.items[0].items[0].items[0].items[1].items[1]
toSort.items[0].items[0].items[0].items[1].items[2]
...
...
...
toSort.items[1].items[1].items[1].items[1].items[0]
toSort.items[1].items[1].items[1].items[1].items[1]
toSort.items[1].items[1].items[1].items[1].items[2]
Looks like it can be used a bit of recursion, isn't it?
When I'm working with data, I normally have the need of create Arrays or Objects on a loop, let's say "on the fly".
Lets say for example I want to rearrange an array grouping the element by one of the array keys: One of the methods I use is to loop trough an for in. But the problem is when I define more than one index on the fly.
for(key in array){
newArray[array[key]['gouping_key']] = array[key];
}
This example works fine. But if you have more than one element with the same grouping_key, this code is going to override your previous element.
So, I try this:
var i = 0;
for(key in array){
newArray[array[key]['gouping_key']][i] = array[key];
i++
}
But when I add that second index the interpreter complains saying that the newArray[array[key]['gouping_key']] is undefined. Problem it doesn´t seems to have on the previous example.
Why is that?
I've made this fiddle with an example in case the previous snippets an explanation would be insuficient and unclear. In the fiddle you have three snippets (two commented out).
The first one is the error I get when trying something like what Iǘe mentioned previously.
The second is the solution I use.
And the third an example of the creation of an array on the fly with only one index.
Summing up, I want to know why, when you add the second index, you get that error.
Thanks!
var i = 0;
for(key in array){
// If nested object doesn't exist, make an empty one.
newArray[array[key]['gouping_key']][i] =
newArray[array[key]['gouping_key']][i] || [];
newArray[array[key]['gouping_key']][i] = array[key];
i++
}
You need to create an array to push to, it's not created for you. You can use the || operator to only create an empty array if it's undefined.
Also, that's a lot of nesting to follow... If I may...
var x, y;
y = 0;
for(key in array){
x = array[key].gouping_key;
newArray[x][y] = newArray[x][y] || []
newArray[x][y] = array[key];
y++
}
See how much more readable that is? It's also faster! You dont have to deeply traverse complex objects over and over again.
First using for in for arrays is no good idea, it does not what you are expecting I think. see: https://stackoverflow.com/a/4261096/1924298. Using a simple for loop (or while) should solve your problem.
I hope you can help me with this hopefully stupid problem.
I try to do the following:
creating array with data
looping through this array within a for loop (based on array.length)
create new object based on data in array
So far I got the following:
create array
loop through array
create one object based on my constructor
The problem is, the array has a length of 4 and should therefore create 4 objects but it creates only one. If I remove the creation of the object and just log "i' it works, but in the original intention it ends after the first
The loop looks as follows:
for(i=0;i<array.length;i++)
{
newObj[i]=new ObjectName(array[i].param1,array[i].param2,array[i].param3)
}
I have no idea why it ends after the first run and I also don't get an error displayed when looking into firebug.
Cheers
Does changing the
newObj[i] =
to
newObj.push(...)
help?
Also how is newObj initialized?
newObj = []
for (i = 0; i < (stringNums.length); i++) {
Dictionary[stringNums[i]] = stringNums[i].length;
}