Iteration Push Alternative Javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Obviously, this is not allowed in javascript:
for (var i=0; i<data.length; i++){
array[i].push(data[i].name);
}
Does anyone know of a workaround that does the same thing?

You can do
for (var i=0; i<data.length; i++){
array[i] = [];
array[i].push(data[i].name);
}
https://jsfiddle.net/zv0xfzzs/

but Alexey Ayzin, shurely this is the better alternative - create a single array, push the desired values into it and then access it via normal means::
arrayTest = [];
for (var i=0; i<data.length; i++){
arrayTest.push(data[i].name);
}
This will give arrayTest all names, and can be accessed as:
var testName=arrayTest[3];
Doing it the other way will give you lots of unnamed arrays - how are they to be used?

Related

I want to convert array to object in java script like[ ["key1","ans1"],["key2","ans2"] ]=> {key1:"ans1",key2:"ans2"} [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to convert array to object in javascript like
[["key1","ans1"],["key2","ans2"]] => {key1:"ans1",key2:"ans2"}
Here you go;
let res = {}
let arr = [["key1","ans1"],["key2","ans2"]]
for (let i = 0; i < arr.length; i++) {
res[arr[i][0]] = arr[i][1]
}
console.log(res)
please comment if you need explanation.

How to shift a index in an array 3 positions to the right in javascript using only loops [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
how can i shift the index's of an array 3 positions to the right , and looping the index's that reach the end of the array back to the begining of the array.
Using only loops and no functions.
Array=[5,10,30,60,50,30,20,2,5]
Thanks
This sounds suspiciously like a homework question :)
var myArray = [5,10,30,60,50,30,20,2,5];
for(var x = 0; x < 3; x++){
myArray.push(myArray.shift());
}
console.log(myArray);
If for some reason you can't use push/shift/pop/unshift
var myArray = [5,10,30,60,50,30,20,2,5];
var newArray = [];
for(var x = 0; x < myArray.length; x++){
newArray[x] = myArray[(x + 3) % myArray.length];
}
console.log(newArray);
You can remove the first element and then continuously add it on the end.

Quoting a list of comma separated string literals? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a list of literals like this (These are unquoted strings - not variables):
alice-blue, antique-white, aqua, ...
And I want them to be quoted like this (For use in a nunjucks loop):
'alice-blue', 'antique-white', 'aqua', ...
Look for a simple way to do this in javascript.
var str = "alice-blue, antique-white, aqua";
var arr = str.split(', ');
for(var i=0; i< arr.length;i++){
arr[i] = "'"+arr[i]+"'";
}
var resultStr = arr.join(", ");

Parsing JSON in javascript for multiple JSON objects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Should be quite a common answer but I haven't found it.
Using client-side javascript:
My client receives some JSON string:
response =
[
{"id1":"value1" , "time1":"valuetime1"},
{"id2":"value2" , "time2":"valuetime2"}
]
I understand that I can simply parse the JSON string with this command:
response = JSON.parse(response);
But what is the next command to access each of the objects individually?
I would prefer to not use jQuery.
In plain JavaScript, just use a simple for loop:
for(var i = 0; i < response.length; i++){
console.log(response[i]); // Object with id and time
}
-or-
response.forEach(function(object){
console.log(object);
});
Demo
This JSON will become the appropriate JavaScript object. In this case, that is an array which you can access the same way as you would any other JavaScript array:
for (var i = 0; i < response.length; i++){
console.log(response[i]);
}

how can I call an unnamed instance in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So if I make a class, and then create new instances of that class without naming them -maybe with a loop that creates a bunch of instances- how can I call a specific (or unspecific) instance? For example, if I'm generating a bunch a squares, but I want to move a specific one somewhere, how would I do that?
Sorry if this is a total noob question, or if I miss-used some terminology, but I'm pretty new to programming.
Example code:
function example(x){
this.x = x;
}
for(var i=0; i<10; i++){
new example(1);
}
//now how would I get a specific instance of examples to have x = say, 10.
You could put each square in an array and access them that way:
function Square(i){
this.index = i;
}
Square.prototype = {
constructor: Square,
intro: function(){
console.log("I'm square number "+this.index);
}
}
var squares = [];
for(var i = 0;i < 10;i++){
squares.push(new Square(i));
}
squares.forEach(function(square){
// do something with each square
square.intro();
});
Demo: http://jsfiddle.net/louisbros/MpcrT/1/

Categories

Resources