Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
I’m using Cypress to get a list of 10 cities from Wikipedia, store them then pick the sixth value in the list. But the below shows the console log as undefined.
I don’t understand it, what am I doing wrong?
It’s not an asynchronous issue it still doesn’t work when the console.log is within the .then
/// <reference types="cypress" />
it('Go to', function () {
cy.visit('https://en.wikipedia.org/wiki/List_of_cities_in_the_United_Kingdom#List_of_cities');
var citiesArray = [];
for (var a = 0; a < 10; a++) {
var num = Math.floor(Math.random() * 77)
cy.get('tbody tr').eq(num,0).find('a').eq(0).invoke('text').then(message => {
citiesArray.push(message);
});
}
console.log(citiesArray[5])
})
I want to log the 6th value in the array but for some reason it shows undefined. How do I access the 6th item in the array?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am having trouble getting the property filename from the req.files that I get from the router. Here's what I get:
And here's how I've been trying to get the filename of each (I am only using 2 pictures in this example but I could get more than two images so that's why I am iterating with the forEach)
let arrayImages = [];
if (req.files) {
Array(req.files).forEach(image => {
arrayImages.push(image[0].filename);
})
}
Hi everyone thanks for all the help, i finally figured it out!
let arrayImages = [];
for (const clave in req.files) {
array = req.files[clave]
arrayImages.push(`${array[0].filename}`);
}
that way i've got the fieldname of each element
The root of your data from screen is an object.
So try to code like that:
let arrayImages = [];
if (req.files) {
Object.values(req.files).forEach(arr => {
arrayImages.push(arr[0].filename);
})
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Is it possible to manage object's properties via set get in defineProperty?
I'm not sure that I'm using this sentence properly.
<div id="app"></div>
why
<script>
var div = document.querySelector('#app');
var viewModel = {};
Object.defineProperty(viewModel, 'str' , {
get: function() {
return console.log("access");
},
set: function() {
return console.log("setting");
}
})
</script>
I assume that you have run viewModel.go in console - you will get "access" printed in console, but later you will get undefined as it is a result of this get function:
function() {
console.log("access");
}
This function doesn't have a return clause, so value of go will be undefined.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have written:
var arr = [];
for (i = 0; i < 5; i++) {
var name = prompt('please, enter your name');
arr.push(name);
}
console.log(arr);
But it is does not work. It asks me only once, but I need to make array with five names.
It worked for me. Are you using Google Chrome to test. Maybe something for above it is interrupting. Try it on a new Javascript File. If you're using Chrome to test what is the log saying Ctrl + Shift + J let me know.
Thanks :)
It works ok, it is maybe bug in jsbin.com. Sorry.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
When trying to push items to an array in javascripts it gives an error, the following pseudo code illustrate what is happening:
var data = new Array();
for(...) {
data[key].push(item[i]);
}
It is showing the following error:
Cannot read property 'push' of undefined
Thanks
If you need a 2d array, you have to initialize each element of the outer array to be an array.
// Have to check every array item, if it's not an array already, make it so
for(...) {
if (!data[key]) {
data[key] = [];
}
data[key].push(item[i]);
}
You could always do the following if you know the number of inner arrays you need:
var data = [[],[],[],[],[],[]];
In your example, since they variable name is key, I'm assuming you actually want an object of arrays. If you know the keys ahead of time, you can use the following literal.
var data = {
myKey1: [],
myKey2: []
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to map a user defined key to the associated value and it is currently returning undefined for the final (var final). The goal is to collect a user provided input (key) and map it to the associated string from the key:value object and then load a URL using this value. The web page loads incorrectly in the end because the value is undefined. Here is the code:
document.getElementById("btn").onclick = function onLoad() {
var converts = {
'apples' : 'green',
'sky' : 'blue'
};
var myTextField = document.getElementById("myTextarea");
var itemName = myTextField.value;
var final = converts[itemName];
if (document.getElementById('rad1').checked) {
window.open("somewebsite" + final + "restofURL", "this is a new window");
}
}
The problem is that the textarea has a lot of white-spaces in it by default. If the user adds a space, that could cause problems so we need to trim the string:
convert[ itemName.trim() ];
This will remove all the bordering whitespace.