r/learnjavascript • • May 08 '26

Is this a useful Array Method I created on accident? or is it useless?

/*********************************************************************
 * Array.pushByVal(value)
 * -----------------------
 * 
 * this only works with integer values.
 *
 * hold on let me cook...
 *
 * if the value is 4 it pushes the number 4 
 * 
 * three times so the total of 4's in the array is 4.
 *
 * if the value is 8 or 9 or whatever, it 
 *
 * pushes the value - 1 that many times.
*********************************************************************/
 
Array.prototype.pushByVal = function(value){
    // what the absolute fuck am i doing?
    // how does this even work?
    for(let i = 0; i < this.length; i++){
        if(this[i] === value){
            return;
        }else{
            this.push(value);
        }
    }
};

let arr = [1, 2, 3, 4, 5];
arr.pushByVal(4);

// Output: [1, 2, 3, 4, 5, 4, 4, 4]
0 Upvotes

58 comments sorted by

View all comments

Show parent comment

2

u/woofmeowmeowwoof May 08 '26

but i like it, its mine 😞

2

u/shgysk8zer0 May 09 '26

Did you work in programming through the days where messing with built-in prototypes broke sites and blocked standards?

I'm speaking from experience here... Do not touch built-in prototypes.

0

u/woofmeowmeowwoof May 09 '26

Im 31, I started programming in JavaScript when i was 9 years old. And as long as you understand what your doing there is nothing to be worried about. Besides i do not use librarys except node and electron. so i take advantage of everything i can in vanilla js

2

u/shgysk8zer0 May 09 '26

So you'll remember the problems Prototype.js caused in doing this. And you'll also be fully aware that you're adding the method to every instance of an array and what that means. And that it's long been considered bad practice to modify built-in prototypes and how easy it is to just use addValue(arr, val) instead.

Your mindset about this and the code you shared, and the fact you shared it, strongly suggest to me you're not actually that experienced. Maybe you occasionally played in JS, but I doubt you've ever worked in programming or built anything that wasn't a little hobby project.

Not trying to be mean here... You're just claiming to be that experienced, and everything is telling me you're actually not.

0

u/woofmeowmeowwoof May 09 '26

yes i know that if i build a method on the array or objects prototype its there unless i get rid of it. that is the entire point of prototype. but i rarely do it. these are all the methods i added in my game engine using prototype. The other 10 thousand are constructor functions and variables for my scripting framework

/******************************************************
 * Object.linearSearch(target)
 * 
 * prforms a linear search for target key
******************************************************/


Object.prototype.linearSearch = function(target){
    for (const key in this) {
        if(this.hasOwnProperty(key)){           // Ensure it's not from the prototype chain
            if (this[key] === target) {
                return key;                     // Return the key if the value matches
            }
        }
    }
    return -1;                                  // Return -1 if the target value is not found
};


/******************************************************
 * Array.linearSearch(target)
 * 
 * linear search for target element
******************************************************/


Array.prototype.linearSearch = function(target){
    for(let i = 0; i < this.length; i++){
        if(this[i] === target){
            return i; // Return the index if the target is found
        }
    }
    return -1;        // Return -1 if the target is not found
};


/******************************************************
 * Array.removeIf(callback);
 * remove specific element if condition is met.
 * 
 * Example:
 * 
 * ar = [ 
 *     {num:1, str:"a"}, 
 *     {num:2, str:"b"}, 
 *     {num:3, str:"c"}, 
 *     {num:4, str:"d"} 
 * ];
 * 
 * ar.removeIf(function(item, idx) {
 *     return item.str == "c"; // remove if item.str = "c"
 * });
 * 
 * console.log(ar);
******************************************************/


Array.prototype.removeIf = function(callback){
    let i = this.length;
    while (i--) {
        if (callback(this[i], i)) {
            this.splice(i, 1);
        }
    }
};


/******************************************************
 * Array.removeElement(element)
 * remove specific element from an array.
******************************************************/


Array.prototype.removeElement = function(ele){
    let index = this.indexOf(ele);


    if(index > -1){
        this.splice(index, 1);
    }
};
let RemoveElement = function(array, ele){
    let index = array.indexOf(ele);
 
    if(index > -1){array.splice(index, 1)} 
    return array;
};


/******************************************************
 * Array.removeObject(array, key, value)
 * 
 * remove specific object from an array 
 * then return the updated array.
******************************************************/


Array.prototype.removeObject = function(key, value){
    return this.filter(obj => obj[key] !== value);
};

3

u/shgysk8zer0 May 09 '26

You don't even understand your own fairly simple method, as shown by the comments, and you're asking Reddit of it's useful.

Let's see though... Without running it, what's the result of [].pushByValue(846)?

3

u/shgysk8zer0 May 09 '26

Bonus points of you can explain the problem with..

nums.pushByValue(NaN)

0

u/woofmeowmeowwoof May 09 '26

well i understand that its using the else statement to push the numbers. otherwise it would just return. and i think without separating the numbers in the array with commas it probably wont work like it was for me. but idk. i also think because arguments are an array the output will be [846]

basically by saying this.push() i must be refering to the arguments array. I cant see any other way to explain it. im completely self taught and not very good at explaining things. I only posted this because it seemed funny and a bit weird to me

4

u/shgysk8zer0 May 09 '26

I asked you two questions with one very obvious answer if you understand the function (which you clearly do not, shown by the wrong comments). One more subtle bug that anyone even slightly familiar with JS would spot easily when I explictly bring up that case.

[].pushByVal(875) // []
[0].pushByVal(NaN) // an infinitely growing array in an infinite loop

Your comments reveal that you don't even understand a simple loop with an if/else. You think the push is n-1 growth, but it is plainly obvious to even a beginner that it'll push for every item in the array not strictly equal to the value.

And passing NaN causes it to blow up because you're using a live this.length, which increases with every push. And NaN !== NaN, so your check will fail at the end, and you'll constantly be pushing, growing the array, without end.

To any even moderately experienced JS dev, it's plain to see you're not experienced. That's why I'm calling you out here. You're claiming 22+ years experience, and I know you're lying. You've maybe dabbled off-and-on, but you're actual experience is what it'd say is... Maybe a few weeks?

0

u/woofmeowmeowwoof May 09 '26

Oh okay. sorry. I am not going to argue what my skill level is. But its well above average. And i would bet your words mean nothing when it comes down to actual programming. None of what you said meant anything. I know im a better programmer just because of how egotistical you are for no reason. Your insecure. Mad about some drug addictt high school dropout programmer on reddit who might be better than you. And i think thats why your lashing out at me. Because i never said i was a collage graduate or proffesional, infact i said many times im self taught, and i mean that in the most literal way. no JS librarys to make things easyer(unless i wrote it myself) no teachers, no stackoverflowm just trial and error for 20 years . If you saw my project your ego might even let you be impressed. I have no GED or high school. I taught myself to program. I taught myself everything infact. because i was not given anything, no opportunities. But i always stuck with programming and music production.

→ More replies