testing for all values in an array over a certain value - javascript

I have an array of values that are produced from an equation.
e.g.
testArray = [1,2,3,4,5,6,7,8,9,10];
What I want to do is write a function to look for all the values over a particular value and output the result.
So for example, in the above testArray how would I return all the values over 7 (8,9, 10 only not including 7)?
Happy for the response to use Javascript and/or Jquery.
Thanks in advance.

You could use filter().
var testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter(function(element) {
return element > 7;
});
jsFiddle.

Related

VSCode automatically formats console.log() output in terminal when logging an array that is longer than 5 elements

I'm trying to log out an array in the terminal but it automatically formats it and adds a new line if the array is longer than 5 elements. How do I make it print normally in a single line.
Example code:
const someArray = [1,2,3,4,5,6,7]
console.log(someArray)
Result:
Result of console.log
It can be annoying sometimes, this is my solution to fix it when I need to print something line by line.
const someArray = [1, 2, 3, 4, 5, 6, 7, 8];
// The last parameter 4 is for indentation.
console.log(JSON.stringify(someArray, null, 4));
This can solve your purpose.
const someArray = [1, 2, 3, 4, 5, 6, 7]
console.log(someArray.join(", "));
Simple use JSON.stringify(array);
example :
const array = [2,3,2,23,3,3,5,2,3,4,2,3,4,3,4,2];
console.log(JSON.stringify(array));

Accessing and returning nested array value

I can't seem to figure this out. I just get an undefined return.
let test = [1, 2, 3, 4, [6, 7, 8]];
How do I return the index [2] of test[4]?
I'm not even sure I'm asking the question properly.
Basically, I want to interact with 8.
To maybe help you understand what is going on.
If you write
let test = [1, 2, 3, 4, [6, 7, 8]];
you create an array (which is more like a list if you compare it to other languages). Every entry has its own datatype. So in the example we have the first 4 elements which are just numbers and the fifth entry which is another Array.
With the [] operator we address certain elements inside the array. If we want the first entry we can use test[0] and should get back 1.
You now want to access an element in the array inside an array. So you first address the array in the array. test[4] this will give you back [6, 7, 8] and now you can do the same thing again and address this new array. You could write it this way
let test = [1, 2, 3, 4, [6, 7, 8]];
let innerArray = test[4];
let element = innerArray[2];
The example above is just to better understand what is going on. In practice you will just do test[4][2] and it will basically to the same as above.
Try this.
let test = [1, 2, 3, 4, [6, 7, 8]];
console.log(test[4][2])

Remove an item from an array by value, but leave the duplicates of that item

I have a very simple array like this:
array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
I need to be able to remove a value, but I need to remove only one value if there's duplicate values. So if I remove the value 6, the array should become:
array = [1, 1, 7, 9, 6, 4, 5, 4];
The order of which one gets removed doesn't matter, so it could be the last no. 6 or the first no. 6. How can I do this?
Edit
I see there's a lot of confusion about why I need this, which results in incorrect answers. I'm making a Sudoku game and when a user inserts a number in a cell, the game has to check if the chosen number already occupies space in the same row or column. If so, the number of that specific row/column is added to this array. However, when a user fixes a mistake, the number of the row/column should be removed. A user can, however, make multiple mistakes in the same row or column, which is why I need to retain the duplicates in the array. Otherwise, users can make multiple mistakes in a row/column, and only fix one, and then the code will think there are no errors whatsoever anymore.
Hope this makes things more clear.
Try to get the index of your item with indexOf() and then call splice()
let array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
let index = array.indexOf(6);
array.splice(index,1);
console.log(array);
var array=[1, 1, 6, 7, 9, 6, 4, 5, 4],
removeFirst=function(val,array){
array.splice(array.indexOf(val),1)
return array;
};
console.log(removeFirst(6,array));
You can use Array.prototype.findIndex to find the first index at which the element to be removed appears and then splice it.
Also you can create a hastable to ascertain that we remove only if a duplicate is availabe - see demo below:
var array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
var hash = array.reduce(function(p,c){
p[c] = (p[c] || 0) + 1;
return p;
},{});
function remove(el) {
if(hash[el] < 2)
return;
array.splice(array.findIndex(function(e) {
return e == el;
}), 1);
}
remove(6);
remove(7);
console.log(array);
If order of removed element (not elements!) isn't important, you can use something like this:
array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
function remove_if_dupe(elem, array) {
dupes=[];
for(i=0;i<array.length;i++) {
if(array[i] === elem) {
dupes.push(elem);
}
}
if(dupes.length>1) {
//is duplicated
array.splice(array.indexOf(elem), 1);
}
return array;
}
console.log(remove_if_dupe(6,array));
This should keep unique elements, hopefully.

How come I can't use the length number from this array?

I created an array, and when I try to get the length of the array it works fine.
var map = [
[3, 0, 0, 2],
[7, 6, 6, 8],
[7, 6, 6, 8],
[5, 1, 1, 4]
];
var i = map.length;
i outputs 4.
When I try to use the i variable to get the column using var j = map[i].length; the console returns "map[i] is undefined". How come this won't work, but replacing i with an actual number works?
Here is an example jsfiddle, just uncomment line 11.
i is equal to 4, as you said. JS array indices start from 0, so the last element in your array is map[3] which means there is no element at map[4]
You need to do map[i-1] - this code should work:
var j = map[i-1].length;
And here is it working in your jsfiddle: https://jsfiddle.net/zk7f8Ls2/2/
Because table index are zero-based. The table length is 4 but indexes are 0, 1, 2 and 3. When you try to access index 4, you will get an error.
It's because i is 4, and remember that arrays start with 0 if you want to see the last item of the array just add -1 map[i-1]

Javascript:: Find differences between array that contains duplicated values to a unique array of that array

I found many posts on stack overflow about that similar subject but none of them solve this issue here.
<script>
//Array GanginaA contains duplicated values.
//Array GanginaB contains only unique values that have been fetched from GanginaA
GanginaA=[0,1,2,3,4,5,5,6,7,8,9,9];
GanginaB=[0,1,2,3,4,5,6,7,8,9];
var hezi=<!--The Magic Goes Here-->
console.log(hezi);
/*
* Expected Output:
* 5,9
*/
</script>
GanginaA will always be longer or identical to GanginaB so there is no reason to calculate by the value of the longer array length.
GanginaB will always contains unique values that taken from GanginaA so it will always be the shorter array length or identical to GanginaA array.
Now it makes it a lot easier to find doubles.
You can use filter to get the elements like below
GanginaA = [0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9];
GanginaB = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var hezi = GanginaB.filter(function (item, index) {
return GanginaA.indexOf(item) !== GanginaA.lastIndexOf(item)
});
console.log(hezi.join(" , ")); // 5, 9
the easier I can think of :
var hezi=[];
for (var i=0;i<GanginaA.length;i++){
hezi[GanginaA[i]] = GanginaA[i];
hezi[GanginaB[i]] = GanginaB[i];
}
hezi = hezi.filter (function(el){return el!=undefined;});
does everything in O(n) actions and not O(n^2)
Javascript's objects have hashmap like behaviour, so you can use them kind of like a set. If you iterate over all the values and set them to be keys within an object, you can use the Object.keys method to get an array of unique values out.
function uniqueValues() {
var unique = {};
[].forEach.call(arguments, function(array) {
array.forEach(function(value) {
unique[value] = true;
});
});
return Object.keys(unique);
};
This function will return the unique elements in any number of arrays, passed as arguments.
uniqueValues([1, 2, 3], [ 1, 1, 1], [2, 2, 2], [3, 3, 3]); // [ 1, 2 3 ]
One drawback to this method is that Javascript coerces all keys to strings, you can turn them back into numbers by changing the return statement to:
return Object.keys(unique).map(Number);

Categories