A bit of a noob to JavaScript OOP here. I have a native JavaScript object that I intend casting as an array and using it as an array but I can’t understand why it doesn’t work. I have a form like <input type="text" id=input />
Then I have my jQuery like
var vars = {about: "about", bunny: "bunny", case: "case"};
$("#input").on("input", function(){
vars = Array.prototype.filter.call(vars, function(b){
console.log(b);
return b.indexOf($(this).val()) != -1;
});
});
It says “Array.prototype.filter called on null” and I can’t figure out why
I need the keys as well. Specifically, I need to be able to request for value by key instead of by value directly and JavaScript does not support associative arrays so I’m stuck with objects. Casting objects as arrays however does nothing when I attempt using the array methods forEach not filter. Why is this so and how do you suggest I access keys instead? For-in?
My confusion is why this ^^^ does not treat it as an array. An object is an iterable data structure/type so ideally, it should be able to iterate and behave normally. I have used DOM objects extensively with the dozens of jQuery methods available and I’m just surprised this simple object to array casting is seemingly impossible in JavaScript.[quote=“Dormilich, post:4, topic:227790”]
$.each()
[/quote]
Will each let me return an object containing filtered off values as the jQuery filter method works different from the native JS filter method?
objects do behave normaly when iterated. it’s just that array methods are not iterators. iterable is not the same as an array, an array is way more restricted than an iterable. and what is “normal” in JS is defined in the specification, not by us.
[quote=“Paul_Wilkins, post:9, topic:227790, full:true”]
Instead, we’ll reduce the keys and add to them to a new object. That way we can assign the new object to whatever we want.[/quote]
We can improve that code further by pulling the function out to a separate wrapper:
var vars = {about: "about", bunny: "bunny", case: "case"};
function keyContainsValue(value, sourceObj) {
return function reduceToKeysMatchingValue(callback, key) {
if (key.indexOf(value) > -1) {
callback[key] = sourceObj[key];
}
return callback;
};
}
$("#input").on("input", function() {
var input = this;
var result = Object.keys(vars).reduce(keyContainsValue(input.value, vars), {});
console.log(result);
});
While I truly appreciate your contributions here, I will solicit your code improvement skills on my other AJAX thread just created on this board not too long ago. Thank you.
I know this is an old topic but for myself and for anyone else who might need this code, I hope to explain better so even though it works, you can know why it does:
I hope it is clearer now and explaining it this way makes it stick for me too.