How to use the for loop in this situation [closed] - javascript

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 2 years ago.
Improve this question
I have data like this:
data = [
{ 'name':'suresh', 'age':25}
{ 'name':'ramesh', 'age':35}
{ 'name':'raina', 'age':15}
]
I have tried like
for(let ele in data){
consolelog(ele)
}
Current Output
0
1
2
Expected Output
{ 'name':'suresh', 'age':25}
{ 'name':'ramesh', 'age':35}
{ 'name':'raina', 'age':15}

You have to use the of keyword for looping arrays
for(let ele of data){
console.log(ele)
}

It's because by doing this:
for(let ele in data){
consolelog(ele)
}
You're looping the properties of the array. An array is really just an object with the properties 0, 1, 2, ...
There are several ways to loop an array. In my opinion the two most common are:
// old-school...
for (let i = 0; i < data.length; i++) {
console.log(data[i]);
}
// ... new-school (ES5+)
data.forEach(ele => console.log(ele));

By your approach
let data = [
{ 'name':'suresh', 'age':25},
{ 'name':'ramesh', 'age':35},
{ 'name':'raina', 'age':15}
];
for(let ele in data){
console.log(data[ele]);
}

Try this code this work for me
for (let ele in data) {
console.log(data[ele])
}

Related

How to nagivate an array based on another array in JS [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
Here are two arrays:
array1=['alex','joe','simon','sarah']
let index1= 0;
array2= [ [1,2,3,4,5,6,7],[1,2,3] , [1,2] ....]
let index2= 0;
Initially we are in array1 in index 0, I have a next button and whenever user clicks on it index2 moves and its value become 1, then with next click it is 2 and so on and so forth.
The problem is I want to change indeex1 whenever the navigation of the first array of array 2 is finished.
What I mean is when index2 is equal to 6 the index1 should change to 1.
How can I achieve this?
This is just an example
const array1 = ['alex','joe','simon','sarah']
let index1 = 0;
const array2 = [ [1,2,3],[1,2,3],[1,2]]
let index2= 0;
function userClick(arr){
if (arr[index1].length > index2) {
console.log(index1, index2);
index2++;
}
else {
index1++;
index2 = 0;
}
}
for (let i = 0; i < 7; i++) {
userClick(array2);
}

How do I get results? [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 am a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()

Adding values to an array in JavaScript loops [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 2 years ago.
Improve this question
I'm learning JavaScript and I used the loops to add some values to an array and then print theme and see the result. I have done this with different methods but I have a problem with one of them.
In the code below I can't think of a way to add values to an array using for/in the loop. How can I do this?
<script type="text/javascript">
function print(string){
document.write(string+"<br>");
}
</script>
<script type="text/javascript">
print("with for & for..in");
var ma1Arr = [];
for (i = 10; i < 20; i++) {
ma1Arr.push(i);
}
for (i in ma1Arr) {
print(ma1Arr[i]);
}
print("<hr>");
print("with for & for..in vice versa");
var ma3Arr = [];
i=20;
for (i in ma3Arr){
ma3Arr.push(i);
i++
}
for (i=0; i<ma3Arr.length; i++){
print(ma3Arr[i])
}
print("<hr>");
print("with while & do..while");
var ma2Arr = [];
i = 30;
do {
ma2Arr.push(i);
i++;
} while (i < 40);
i = 0;
while (i < ma2Arr.length) {
print(ma2Arr[i]);
i++;
}
print("<hr>");
print("with while & do..while vice versa");
var ma4Arr = [];
i = 40;
while (i<=50){
ma4Arr.push(i);
i++;
}
i = 0
do {
print(ma4Arr[i]);
i++;
}while (i<ma4Arr.length);
</script>
for...in is used for objects like {a: 1, b: 4} and not for arrays. It is iterating all enumerable properties of the object such as a and b. But for arrays, you would need to go with for...of
for..in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
for...of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
var array_name = [item1, item2, ...];
You put the values inside of the brackets... That is basically how you make an array for any language

How to search in localstorage for specific word and get entire key and value? [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 2 years ago.
Improve this question
On click i created localstorage item "storageKey__list" and value is "example.com"
I need to get all item keys and values ends with "__list" and then get entire key and value.
Result must be:
"storageKey__list , example.com"
You could grab the entries in localStorage using Object.entries() and then use .filter() to obtain only the entires ending in "__list" by using .endsWith():
Object.entries(localStorage).filter(([key]) => key.endsWith('__list'));
Output:
[["storageKey__list", "example.com"]]
A more browser friendly version of the code above could be to use the following:
Object.keys(localStorage).filter(function(key) {
return /__list$/.test(key);
}).map(function(key) {
return [key, localStorage.getItem(key)];
});
Output:
[["storageKey__list", "example.com"]]
Try this:
/** #type {[string, string][]} */
const keyValuePairs = [];
for (let i = 0, l = localStorage.length; i < l; i++) {
const key = localStorage.key(i);
if (key.endsWith("__list"))
keyValuePairs.push([ key, localStorage.getItem(key) ]);
}
console.log(keyValuePairs);

How to declare a json object or array collection with variable size 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
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

Categories