test = [
[value1],
[value2]
];
for (var i = 0; i < test.length; i++) {
console.log(test[i])
};
I tried this but it only returns value1. I would like to have all values returned so I can use each value.
Related
I'm working with the problem below:
You are provided with an empty array called nestedArr. Using a for loop starting at index 0, add 5 subarrays to nestedArr, with each nested array containing the string 'loop' concatenated with the corresponding index in nestedArr as its first element, and the index as its second element.
Example of a subarray: ['loop3', 3].
This is what I've tried
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = []
for(var j = 0; j < 2; j++) {
nestedArr[i][j] = 'loop'+[i], i
}
}
console.log(nestedArr)
It gives me [['loop0', 'loop0'], ['loop1', 'loop1'], ['loop2', 'loop2'], ['loop3', 'loop3'], ['loop4', 'loop4']]
Can't figure out how to have it in this format: ['loop3', 3].
Please guide me!
You can just construct the array literally or use a ternary operator to check the first index in the inner loop and concatenate the string loop
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = ['loop' + i, i];
}
const nestedArr2 = [];
for (var i = 0; i < 5; i++) {
nestedArr2[i] = [];
for(let j = 0; j < 2; j++){
nestedArr2[i][j] = j == 0 ?'loop' + i : i;
}
}
console.log(nestedArr, nestedArr2)
const nestedArr = [];
for (let i = 0; i < 5; i++) {
nestedArr.push(['loop' + i, i]);
}
console.log(nestedArr);
I'm trying to make my 2d matrix to have numbers which continue on the new row
var myMatrix = [];
var row = 5;
var colom = 3;
for (var i = 0; i < rows; i++) {
var toto = 1;
myMatrix[i] = [i];
for (var j = 0; j < colom; j++) {
myMatrix[i][j] = [i + j];
}
}
console.log(myMatrix);
I'm trying to make it print numbers like this:
123
456
789 and etc...
but without success:/
can someone help and also give a video or site with examples where i can learn more about that kind of stuff?
First, a look at what your code is doing:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [i];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = [i+j];
}
}
console.log(myMatrix);
You have a typo in your row/rows variable name. Ignoring that though...
Your myMatrix[i] line is creating an array at i, which is then being set to an array with a value of i. Just this creates a wonky mash-up , where each "row" gets an array with it's row number as the first value, something like this:
[[0], [1], [2], [3], [4]]
Your inner loop then adds a value to that array at the place and adds i+j together, but puts that inside of an array, which isn't what you want, so you get something like this:
[
[[0], [1], [2]], // i = 0
[[1], [2], [3]], // i = 1
[[2], [3], [4]], // i = 2
// ... etc
]
Also note that you are replacing that first [i] anyways, so don't set it like that, just make it an empty array [].
What you want is something like this:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = (i*columns)+j;
}
}
console.log(myMatrix);
There were three changes to your code:
Make the [i] and []. It doesn't hurt anything, but [i] also doesn't make sense.
Take the i+j part out of the array, you just want a value there.
When you add i, multiply it by columns so it doesn't reset every time: (i*columns)+j
This will give you a nice output, starting with 0. If you want it start at 1, just add one to your value:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = (i*columns)+j+1;
}
}
console.log(myMatrix);
Use i * columns + j ... and I have to add up to 30 chars for padding
I have an array of data as follows:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]]
I tried to write datas to console for each object as follows, but it does not work:
for (var i = 0; i < 3; i++) {
for(var obj in Sonuc[i]) {
console.log(obj.Number);
};
}
How can I output the Number value for each data on console?
The problem is that you have an array or arrays, with the sub-arrays each containing one or more objects.
Your problem is you are not specifying the index for the sub-arrays. You can access the first object like this:
console.log(obj[0].Number);
That will get you some output at at least, but it is confusing exactly what data you want to get. That 3 loop makes no sense...
If you want to output all objects, then you should first loop the sub-arrays, and then loop the objects. Something like this:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]];
for (var i = 0; i < Sonuc.length; i++) {
var arr = Sonuc[i];
for (var j = 0; j < arr.length; j++) {
var obj = arr[j];
console.log(obj.Number);
}
}
I'm new to javascript and JSON. I have JSON like this:
data = [[{"checked":false,"no":"1"},{"checked":true,"no":"2"}]]
But I am having a hard time looping through just to get the "checked" value.
I used for loop then i try to console.log data.checked but it always returned undefined. How can do this?
Get first array element using index and use Array#forEach method for iterating.
var data = [
[{
"checked": false,
"no": "1"
}, {
"checked": true,
"no": "2"
}]
];
data[0].forEach(function(v) {
console.log(v.checked);
})
You have a nested array, so to access the checked property of the member objects you'll need to access them at data[0].
let innerData = data[0];
for(let i = 0; i < innerData.length; i++) {
console.log(innerData[i].checked);
}
In your JSON, your have a top-level array, then inside that you have another level of array(s), and in that array you have the objects. So what you should do is something like this.
for (var i = 0; i < data.length; ++i) {
for (var j = 0; j < data[i].length; ++j) {
console.log(data[i][j], data[i][j].checked);
}
}
You could get rid of the double nested array with
array = array[0];
Then loop as you always loop.
// This is array in array with object
var data = [[{"checked":false,"no":"1"},{"checked":true,"no":"2"}]]
for(var i = 0 ; i < data.length ; i++){ // Test the length of first array and go inside
for(var z = 0 ; z < data[i].length ; z++){ // When you are inside the array go to another one array and inside go and search 'chacked'
console.log(data[i][z]['checked']); // show value of the checked
}
}
I need help regarding insertion of array elements as objects into another array in Javascript. I have the following code:
tableLength = 3;
nyCourt = [];
oldArr = [Buy, String, Question]
for (var t = 0; t < tableLength; t++) {
nyCourt.push({});
for (var i = 0; i < OldArr.length; i++) {
nyCourt.Title = OldArr[i] ;
}
};
The code isnt working, I want output in the following format
[{Title:Buy },
{Title: String},
{Title: Question}]
But the output I get is this:
[{Title:Question },
{Title: Question},
{Title: Question}]
This line:
nyCourt.Title = OldArr[i]
writes to the Title property on the nyCourt object (which is an array object), repeatedly in the loop. The last assignment wins.
But given what you've said you want your output to be, your code is over-complex. You only need one loop:
var nyCourt = [];
var oldArr = [Buy, String, Question];
for (var i = 0; i < oldArr.length; i++) {
nyCourt.push({Title: oldArr[i] });
}
Live Example (use Chrome or something else modern) | Source
Or as this is Node so we know we have map:
var oldArr = [Buy, String, Question];
var nyCourt = oldArr.map(function(entry) {
return {Title: entry};
});
Live Example | Source
//this give the output you want
tableLength = 3;
nyCourt = [];
oldArr = ['Buy', 'String', 'Question'];
for (var t = 0; t < oldArr.length; t++) {
nyCourt.push({Title: oldArr[t]});
};
console.log(nyCourt);
place that push function inside the loop also change the code like this
for (var t = 0; t < tableLength; t++) {
for (var i = 0; i < OldArr.length; i++) {
nyCourt.push({"Title": oldArr[t]});
}
};