i have some values in the input and i want to save them into an array but first i want to set the "0" index of the array to 0 and then in the 1,2,3,4 indexs add the value of the input but i can get to make it
before to set the values of the input i set the 0 to the position 0 of the array but it doesnt work
the thing is that i want to use only the first four number of the input_data into an array but first set the 0 position to 0 and then add 5,10,15,20.
so the final array would be 0,5,10,15,20
i set the index "0" of the array to 0 and the other indexes i fill them whit the incoming data.
This depends on how the data is reaching the 'Modified Java Script Value' step.
If it is one single row, with the string containing a "CSV", then Rohit.007 answer will suffice.
If you have multiple rows reaching the step, the Script will Repeat N(row) times. If you have 4 rows being fed to the step, this script will run 4 times, so you need some sort of restraint on the Variables, so you don't repeat some parts of the code.
Personally i would use something like this for Pentaho:
I generated 4 rows, with an add sequence , 1 to 4.
The first run of the script creates the array, pushes 0 and the value of the first row. The other iterations of the script just keep pushing whatever values are found on the specified row to this array (without "Re-declaring" it).
Remenber that the "For Each" command is kind of blurred in pentaho, since you're almost always dealing with multi-row tables, so whatever you do in scripts has to have some constraints on variables declarations.
You can try the below code.
let array = [];
array.push(0);
let string = '1,2,3,4';
array = array.concat(string.split(','));
let result = array.map(function (x) {
return parseInt(x, 10);
});
console.log(result);
Related
I have a program that displays errors like this:
wifi : 298
So, it says the type of error and the number.
Right now there are 8 errors, which I have divided by type and number, so 16 values. I would like to add them to an array like:
var array{type:'____', number:'____'}
I would like to add the values via function, so it can be automatic, in case I add ou remove errors to display, a for seems the best way to do it.
Problem is.. I'm horrible at math..
I've tried a for where i starts at -1 and its i++. The type would be value[i+1] and the number would be value[i+2].
This would be the result:
i=-1
type=value[0]
number=value[1]
i=0
type=value[1]
number=value[2]
So you see, value[1] appears twice:
0
1
1
2
When it should only appear once:
0
1
2
3
var errorSplit = errorlog.split(/:|;/);
var errors;
for(var i=-1;i<16;i++){
errors= {type: errorSplit[i+1], num: errorSplit[i+2]};
}
Just up it by 2 on every iteration. I would also suggest slight changes for readability (Replacing -1 by 0 and changing index acccessors).
You need to make the errors array an actual array and add things to it not overwriting them.
You can make the loop variable length too by just using the errorSplit length as a limiter.
var errorSplit = errorlog.split(/:|;/);
var errors=[]; // Make the array an actual array
for(var i=0; i<errorSplit.length; i+=2) { // Start at index 0, go until there are no errorSplit parts left, up it by 2 every iteration
if(errorSplit.length<(i+1)) // If the index is higher than errorSplit+1, exit the loop
break;
errors.push({type: errorSplit[i], num: errorSplit[i+1]}); // Add the element to the array
}
This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 4 years ago.
I couldn't find a question that specifically targets the issue I'm having hence this question is being asked.
I have an array that holds 5 numbers:
var numbers = [0,1,2,3,4];
Once a number is clicked on the frontend (website), the number is removed from the array using the below code:
delete numbers[1];
This removes the correct number but leaves a space where the number was (the space is undefined). I believe this is causing an issue. After a number is removed from the array, I use another function to randomly pick any of the remaining numbers in the array however it sometimes fails. After much thought, I've realized it may be because there are empty spaces in the array after a number is removed and as a result, the code fails to work due to the undefined element.
Is my analogy correct or am I mistaken?
(I have also attempted to use the splice method to remove the number however that then causes an issue with the length of my array because if I later want to remove another number, it removes the wrong one due to the numbers moving around etc).
What you'd want to use is splice
In your specific case, numbers.splice(1,1)
You're correct that delete replaces one of the values in the array with undefined, and does not change the array length. Later on when you randomly choose an element from the array, you can wind up getting that undefined value, because it's still taking up a slot in the array:
var numbers = [0,1,2,3,4];
delete numbers[3];
console.log(numbers)
Instead use splice, which removes the item from the array completely:
var numbers = [0,1,2,3,4];
numbers.splice(3,1) /// remove one element starting at index 3
console.log(numbers)
if I later want to remove another number, it removes the wrong one due to the numbers moving around
You do need to choose one behavior or the other. If you need to preserve indexes as is, then continue to use delete, leaving the undefined values in the array, and rewrite your "choose one at random" function to never pick undefined values:
// start with some undefined values:
var numbers = [0, 1, undefined, undefined, undefined, 5]
var pickRandom = function(numbers) {
// make a copy of the array, removing undefined elements:
var definedValues = numbers.filter(function(item) {
return item !== undefined;
});
if (definedValues.length === 0) {return false}
//choose one at random:
return definedValues[Math.floor(Math.random() * definedValues.length)]
}
// test it:
console.log(pickRandom(numbers));
console.log(pickRandom(numbers));
console.log(pickRandom(numbers));
console.log(pickRandom(numbers));
(...but note that this suggests that a simple array is the wrong data structure to use here; you may be better off with an array of objects each with an explicit ID, so you can reference specific ones as needed without worrying about keeping the array index the same.)
If you mean to actually remove the element from the array, leaving your array with 4 elements, then you can use
numbers.splice(1);
This will remove the element in the index 1 from the array and return the section of the new array.
I have an array which has empty fields.
Particulary it looks as following:
array() {[4] => 3,[8] => 6,[17] => 24}
I am using it this way because I am drawing a graph with it. So for an example: at 4cm the graph has a height of 3cm. And so on.
Now the problem is that the formula only calculates half of the values, since the second part of the graph is just mirrored. So I need to remove the last part of the array and then reverse the rest, while maintaining their distance from that removed value in the original array.
I need a algorhythm which can calculate this for me.
array() {[26] =>6, [30] => 3}
I think an array not a great datatype for this, since you might have potentially thousands of undefined values in it, but filling the array with the missing values is rather easy. You just traverse the array backwards and push the values encountered to the array.
var ar = [/*your input*/];
/* ar.length-1 would be the last position of your array
so you want to go from position ar.lenght-2 */
for (var i = ar.length-2;i>=0;i--){ar.push(ar[i])}
It might be better to use an object instead, so you can work with key value pairs representing your graph nodes.
not sure if it works perfectly, but follow the logic. Lets say that your original array is a
.
a.fill(0).concat(a.reverse().slice(1));
First reverse the original array, and remove the first element (which is the last element of the original array). Then concat the filling zero original array(delete all value, you can fill undefined or other value) with the reversed one. Boom, you get your new array.
I have an array of 200 in length.
Each second I have a new number added to the position [0] of the array pushing the other values up one position on the array.
What I need is to determine the max and min value for the entire array each time I feed it a new number. I need to analyze all the 200 values before a new one is fed to the array.
I've managed to do so, but I encountered some trouble discarding the 'old' max and 'old' min, since once they are pushed out of the array, I don't need them anymore.
I found a way of doing this by using the 1st differential and pushing the actual value in to another array. The problem there was that when I have a min or max appearing multiple times. This new array would have them repeated in no particular order and I want just one max and one min.
If you have an array of n values which is updated at intervals by removing the first element and pushing a new one at the end then you can split the algorithm which finds the min/max of the interval in two steps.
Your precondition allows you to make a strong assumption which is the fact that
min(data) = min(data[0], data[1..n-1])
Starting from this assumption you don't need to calculate the min/max on all values at each step.
Let's make an example, suppose like you said, to have 200 values and to have a generic min function able to calculate the minimum of an array, a pair of values or a value and an array. It's metacode, ignore the syntax.
You start by precomputing the minimum and additional support data:
int middleMin = min(array[1..n-1]); // so we skip first
int firstValue = array[0];
int realMin = min(firstValue, middleMin);
Now, when you insert a new element at the end and remove the first two things can happen:
firstValue == realMin ( && firstValue < middleMin), in this case you must find the next higher value on the new array[0..n-1] since you removed the minimum
middleMin == realMin in this case you know that the removed element wasn't the first so you don't need to recompute the global minimum, you just need to update it according to the new inserted element, eg middleMin = realMin = min(realMin, array[n-1])
actually it can also happen that firstValue == middleMin but this falls back to second case since there is another value in the trail array which is the current minimum.
I'm practicing the array section of JavaScript Koan and I'm not fully understanding why these answers are correct. I added my assumptions below if someone could please clarify/let me know if I'm wrong :
it("should slice arrays", function () {
var array = ["peanut", "butter", "and", "jelly"];
expect(array.slice(3, 0)).toEqual([]);
Why wouldn't it at least slice "jelly" since the slice begins with
3? How does the cut off of 0 make it empty instead?
expect(array.slice(3, 100)).toEqual(["jelly"]);
If the cut off index goes beyond what currently exists in the array,
does this mean that a new array created from slice would contain all
indexes starting at 3 until the end of the array?
expect(array.slice(5, 1)).toEqual([undefined];
Will it always be undefined if the starting index doesn't exist in the
array?
});
The second argument to Array.slice() is the upper bound of the slice.
Think of it as array.slice(lowestIndex, highestIndex).
When you slice from index 3 to index 100, there is one item (in your case) that has index >= 3 and < 100, so you get an array with that one item. When you try to take a slice from index 3 to index 0, there can't be any items that meet the conditions index >= 3 and < 0, so you get an empty array.
--EDIT--
Also, array.slice() should never return undefined. That's one of the advantages of using it. If there are no matching values in the array, you just get back an empty array. Even if you say var a = new Array() and don't add any values to it, calling a.slice(0,1) will just give you an empty array back. Slicing from outside of the array bounds will just return an empty array also. a.slice(250) will return [] whereas a[250] will be undefined.