The following code takes an array of objects structured like so: {html:whatever number:number value}.
function Org(data){
//array of objects
var Data=data;
for(var i=0; i<Data.length; i++){
var nums=[];
nums.push(Data[i].number);
console.log(nums);}
}
Nums should have be logged to the console as [1,1] on the second iteration when called with: [{html:null,number:1},{html:null,number:1}] but instead is logged as [1] on both the first and second iterations. Why might this be?
You need to move the initialization of num outside of the for loop. Inside it creates for each iteration a new empty array.
BTW, no need for using another variable for data.
function Org(data){
var nums = [];
for (var i = 0; i < data.length; i++){
nums.push(data[i].number);
}
console.log(nums);
}
Or shorter:
var Org=data=>console.log(data.map(e=>e.number));
Related
How to get every variable of loop into an array and call all the variables in some other variable.
I am very new to array. I don't have any idea how to solve this.
Suppose I have an Array[]
and the variable in an array is i, so i1, i2, i3 ....... in
n is the number of times loop will run.
So
for (i=1; i<=n, i++) {
//I need an array called here.
//there will be some code play here
//There will be some value returned after the code it could be text or no.
}
then I want to assign all the values of array into a variable with comma separated
var k = array{} i.e k = "i1,i2,i3,......in"
I try to find on Google but not able to find any solution.
This example is a reference for what I want to achieve actually.
You will declare the array before you enter the for loop. You will then perform your logic and .push() the new values into the array inside of the for loop. After the for loop, you would then want to .join() the array. This will assign the values of your array as a string to your new variable.
var yourArray = [];
for(i = 1; i <= n; i++) {
// some logic...
yourArray.push(newValue);
}
var yourNewVariable = yourArray.join(", ");
While this should be very easy to Google, here's how you do it:
for (var i = 1; i <= k.length; i++) {
var item = k[i];
// ...do something with item
}
When you become more versed in JS, look into using .forEach or .map:
k.forEach(function(item) {
// ...do something with item
});
Is this needed in JavaScript in any context? For example I am trying to copy an array and for some reason when I loop through it and copy the values over one by one the resulting array is missing one value; the only thing I can think of why this is so is because the array I am looping over isn't starting at the beginning.
So, is there a way to reset the internal pointer of an array in JavaScript?
Try:
var myArray = ["aa", "bb"];
var myArray2 = [];
for (var i=0, tot=myArray.length; i < tot; i++) {
myArray2.push(myArray[i]);
}
Can you please let me know if it is possible to create dynamically sets of arrays in JS? I tried some thing like this but didn't work
for (i = 0; i < 3; i++) {
var item[i] = [];
}
item1.push(1);
console.log(item1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
You're almost there. Assuming you're trying to create a two dimensional array (an array of arrays), you just have to declare the top level array and then reference the first level array with [x] array syntax like this:
var items = [];
for (i = 0; i < 3; i++) {
items[i] = [];
}
// Here items is an array of arrays where each first level array entry
// is an empty array. You can then put things into those empty arrays
// You can reference the first level array here
items[1].push(1);
items[1].push(2);
console.log(items[1]); // [1,2]
Suppose I have a global object that looks like this:
var TheFruits = {
323: {},
463: {},
223: {} ..... // can be thousands of properties
}
Basically, the keys are IDs and the values are themselves objects. Now suppose I have an array of IDs that I pass into a function and I want that function to return an array of references to the values that match the IDs of the global object (ie. no deep copy). Something like this:
function GetReferences(TheArrayOfIDs) {
var TheArrayOfReferences = [];
return TheArrayOfReferences;
}
Now I know I can write a for loop that iterates over TheArrayOfIDs and that then loops over the object keys at each iteration but then that's a loop within a loop. So I'm looking for the fastest way of doing it, and jquery is available.
Basically, if TheArrayOfIDs = [323, 463, 223]; then TheArrayOfReferences =[TheFruit.323, TheFruit.463, TheFruit.223];
Thanks.
You don't need a second loop:
var results = [];
for (var i = 0; i < ids.length; i++)
results.push(fruits[ids[i]]);
You have to do only one loop as key look-up is built-in :
var TheArrayOfReferences = TheArrayOfIDs.map(function(id){return TheFruits[id]});
Something like that should work :
var i = 0, l = TheArrayOfIDs.length;
for (i = 0; i < l; i++)
TheArrayOfReferences.push(TheFruits[TheArrayOfIDs[i]]);
In the following code sample i get a strange behavior
var data = ['xxx', 'yyy'];
for (var i in data)
{
var a = i;
var b = data[i];
}
The two first iterations works just fine. I get index "0" and "1" in i, but then it loops one extra time and now the i is "sum". Is this by design or what is this extra iteration used for? The result in my case is always empty and it messes up my code. Is there a way to not do his extra loop?
BR
Andreas
It looks like you (or some other code you've included) have added extra properties onto the Array prototype. What you should be doing is checking to see whether the object you're iterating over actually has that property on itself, not on its prototype:
for (i in data) {
if (data.hasOwnProperty(i)) {
a = i;
b = data[i];
}
}
That said, you should never use for .. in on arrays. Use a regular for loop.
See here for more information: http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
You are looping through an Array, not through an Object. For arrays it's better to use:
for (var i=0; i<data.length; i=i+1){
/* ... */
}
In your loop every property of the Array object is taken into account. That makes the for ... in loop for array less predictable. In your case it looks like sum is a property (method) that's added to Array.prototype elsewhere in your code.
There are more ways to loop through arrays. See for example this SO-question, or this one
Just for fun, a more esoteric way to loop an array:
Array.prototype.loop = function(fn){
var t = this;
return (function loop(fn,i){
return i ? loop(fn,i-1).concat(fn(t[i-1])) : [];
}(fn,t.length));
}
//e.g.
//add 1 to every value
var a = [1,2,3,4,5].loop(function(val){return val+1;});
alert(a); //=> [2,3,4,5,6]
//show every value in console
var b = [1,2,3,4,5].loop(function(val){return console.log(val), val;});
Here's a way to safely iterate.
var data = ['xxx', 'yyy'];
for (var i = 0; i < data.length; i++)
{
var a = i;
var b = data[i];
}
What you are getting is an method coming from extending the Array object, I guess you are using some library where is something like
Array.prototype.sum = function () {...};
Perhaps setting data like this would work better: var data = {0:'xxx', 1:'yyy'};
First of all data is an object. Try to add console.log(a); and console.log(b); inside your loop and you'll see.