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 21 days ago.
Improve this question
const propCSS = resposta['propCSS'];
const listData = getList(cssList);
listData.lista = propCSS;
for (let i = 1; i <= list.length; i--) {
list.push(` "$ {listData}" `);
console.log(lista);
}
I'm trying to add an item to an array in a JSON file, but the result only returns me undefined at position 10.
It seems like an error in your for loop implementation.
You are loopping trough an array starting from the position 1 (let i = 1) till the end of the array, but you are descreasing the iterator (i--).
So your loop goes like: list[1] then list[0] then list[-1]... and an array can't have negative indexes.
You need to increase you iterator (i++) or start your loop from the end of the array till the begining: for (let i = list.length-1; i === 0; i--) { //push() }
Also, is this all of your code? If it is, then list.length and list.push() won't work because there is no list declared. Same thing for the console.log(lista), there's no lista declared.
Related
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
Here is my code, it gives me undefined i have also tried indexof() method
let Numbers = [2,3,1,5,6,7,8 ];`
console.log("Unsorted array " + Numbers);
for(var i=0 ;i<Numbers.length;i++){`
alert(Numbers.findIndex[i]);
}
The below code would work after some changes in your code -
let Numbers = [2, 3, 1, 5, 6, 7, 8];
console.log("Unsorted array " + Numbers);
for (var i = 0; i < Numbers.length; i++) {
console.log(Numbers.indexOf(Numbers[i]));
}
In your case, you were using findIndex() which takes a function and executes it for each element of the array. You were passing it a number which is not correct. Also, the invocation of function you were doing was not correct - use () brackets and not [] brackets for function call.
Also, the i itself is the index. I don't know why you would need to use indexOf to get the index of element which you already know is present at a particular index. This method wouldn't be practical unless your array has duplicates and you need to find the first occurring index number for each element of array.
As a side tip, avoid using alert for such purposes. Stick with console log.
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 6 years ago.
Improve this question
I have a sorted array of objects, each with start and end coordinates, stated relative to a larger range that encompasses all of these coordinates. I would like to create new objects and put them into the array.
JsFiddle link is at the bottom.
Here's a visual representation:
before:
|---------------------------------------|
|-----| |----------| |------|
after:
|---------------------------------------|
|---|-----|-----|----------|---|------|-|
I was trying to use a for loop to find the missing ranges and then splice in the appropriate object as I found them. This creates an infinite loop.
I think that I could feasibly create a temporary array with the filled-in objects, then concatenate that with my original array and sort by start coordinate, but I'd like to do it without having to sort the array again.
Here is a link to a jsFiddle
I think your problem is, you are using splice to "modify" the subfeatures[] array, (adding a new element) but at the same time you are looping over that array, that causes infinite loop, I think your logic is good, instead of using splice, it may be easier to just construct a new array
the commented line are the only modification you have to do.(you also have to consider if the last element not ending at the upper limit)
//var newArr=[];
for (i = 0; i < subfeatures.length - 1; i++) {
//newArr.push(subfeatures[i]);
if ( subfeatures[i].end != subfeatures[i+1].start) {
var feat = {start: subfeatures[i].end, end: subfeatures[i+1].start, type: null};
console.log("A feature should be placed after the current index: "+i+". This feature would have the starting point: "+subfeatures[i].end+" and the ending point: "+subfeatures[i+1].start);
//newArr.push(feat);
}
}
//return newArr;
Splice() is not optimal because you have to move all the later elements in the array every time you find a gap. Here's the solution with a new array that Kossel is suggesting:
var newArray = [];
for (i = 0; i < subfeatures.length - 1; i++) {
newArray.push(subfeatures[i]);
if ( subfeatures[i].end != subfeatures[i+1].start) {
var feat = {start: subfeatures[i].end, end: subfeatures[i+1].start, type: null};
newArray.push(feat);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
A little confused, I have the below code that loops through an array and if the value is not in another array push it in. But whether the indexOf statement evaluates to true or false it gets pushed into the array - not quite sure why that is.
function findUnique(fieldId) {
let uniques = [];
for (let i = 0; i < data.length; i++){
console.log(uniques.indexOf(data[i][fieldId]) === -1);
if (uniques.indexOf(data[i][fieldId] === -1)) {
uniques.push(data[i][fieldId]);
}
}
return uniques;
}
Say there are 2 items in data...
first pass through will log out -1 because it is not in the array, then pushes to array...
second pass logs out 0 because it is in the array but all pushes it into the array...
uniques logs out as ['a', 'a']
Just a typo. You're currently looking for false in your array.
Change
if (uniques.indexOf(data[i][fieldId] === -1)) {
to
if (uniques.indexOf(data[i][fieldId]) === -1) {
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 years ago.
Improve this question
I have a function called BiggerOf(array,value) i need a javascript code.the function calculate how many array element Bigger than that value and print these element?
-Array=[2,3,5,7,9,11,13,15,17,19].
-value=8
Try this
var noArray = [2,3,5,7,9,11,13,15,17,19];
function BiggerOf(arrayVal, val){
for (i = 0; i < arrayVal.length; i++) {
if(arrayVal[i] > val ){
alert(arrayVal[i]);
}
}
}
BiggerOf(noArray, 8);
Is this what you want?
function BiggerOf( array, value ){
var result = 0; // a variable to store the result, it starts at 0
for ( var i in array ){ // iterates the array
if ( array[i] > value ) { // checks if the current array item is bigger then value
result++; // if so, result is incremented
}
}
return result; // returns the result
}
Note: for future questions, please provide the code of what you tried, what problem did you encounter and state your problem as clearly as you can. We are not here to do your homework or guess your problem.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have looked at some other questions and they don't seem to address the particular issue I am encountering. Basically, I want to set the key of an object with a variable and then change that variable. I am using this to dynamically increment the keys in the object.
var x = 0;
var object = {};
object[x] = 1;
x+1;
object[x] = 2;
The above will produce undefined for object[1] and object[2] and 2 for object[x]. I would like the output to be 1 for object[1] and 2 for object[2]. Is this possible?
You've written object[0] = 1; object[0] = 2. If you want the keys to match the values, use matching keys and values.
Hint: Start with x = 1 instead of x = 0.
Next, you've written x+1;, which is a do-nothing statement. If you want to add one to x and assign the result to x, you need to *assign the result back to x`. Any of these will do:
x = x + 1
x += 1
x++
Try this:
var x = 1;
var object = {};
object[x] = 1;
x=x+1;
object[x] = 2;
You don't give the new value to x when adding 1 and also initialize x with 1 not 0 to get your result.