How would I select:
object.id2 == "name1";
with jQuery (instead of looping through all of the objects and finding the one who's id2 matches "name1"), so I could write something like:
$("name1");
or maybe:
$(object + " name1");
or possibly:
$(object).attr('id2','name1');
You can use Lodash:
var namedObjects = _.find(allObjects, { id2: 'name1' });
var myObj = $('[id2="name1"]')
//myObj will be an array if there is more than one element that matches
As seen here: jQuery Selector API
This might not be the best way to do it, but if you insist on doing it with jquery, this would be the way to do it:
var theArr = [{id2:"name0"},{id2:"name1"}];
var myObj = $(theArr).filter(function(){
return this.id2 === "name1";
}).get(0);
console.log(myObj); // Object {id2: "name1"}
http://jsfiddle.net/Tentonaxe/kTxkr/
of course, if you don't have to support IE<9, you can cut jquery out without changing much:
var theArr = [{id2:"name0"},{id2:"name1"}];
var myObj = theArr.filter(function(obj){
return obj.id2 === "name1";
})[0];
console.log(myObj); // Object {id2: "name1"}
http://jsfiddle.net/Tentonaxe/kTxkr/1
I'm not sure I completely understand your question, but if you create an object like this:
var foo = {id2 : "name1"};
You can use a jQuery selector like this:
$(foo)
Related
I have to find and display a selector from HTML presented in array, this works for me using Jquery:
var a = '';
var b = Array.prototype.map.call($('p', a),
function(e) { return e.outerHTML; });
console.log(b)
However I don't want to use Jquery, Id rather use plain Javascript so I tried:
a.querySelectorAll('p')
Not working. Why is that and what else can I try?
You're using querySelectorAll as if it is available in the String.prototype object because the variable a is an empty string.
Try document.querySelectorAll('p');
I think what you want is this:
var b = Array.prototype.map.call(document.querySelectorAll("p"),
function(e) { return e.outerHTML; });
console.log(b);
This way your calling the query selector on the document rater than on an empty string(where the function won't exist).
Try this:
document.getElementsByTagName("p")
Which will return an array of all <p> tags
I have an issue related to converting html inputs names to a javascript object.
For example I have an input:
<input type="checkbox" name="product[1]">
<input type="checkbox" name="product[2]">
and I have javascript code:
var data = {};
$('input').each(function(){
// need to do something like
data[$(this).attr('name')] = $(this).attr('checked');
})
I expect to get data object like this;
data = {
product: {
1: 'checked',
2: 'checked'
}
}
Is this possible without using regular expressions?
Replacing your variables with literal values, you get this:
data["product[1]"] = true;
The square brackets have no meaning as they are inside a string, so you won't get any result.
There are ways around this. You could use eval: eval("data."+this.name+" = "+(this.checked?"true":"false"));
However since eval is best avoided, try this:
var m = this.name.match(/(.*)\[(\d+)\]/);
data[m[0]][m[1]] = this.checked;
Yes in general it is possible. You can do the following:
var noregexp = $(this).attr('name').split("[");
if (noregexp.length==2) {
//should be
var the_name = noregexp[0];
var the_index = noregexp[1].substr(0,noregexp[1].length-1); //this will get the index with removed ]
}
I made this up from my mind. It's not a beautiful solution but one without regexp as you wished.
You can get a data structure the way you need using:
var data = {product: []};
$('input').each(function(){
data.product[$(this).attr('name').match(/product\[([\d]*)\]/)[1]] = $(this).prop('checked');
})
console.log(data);
Check thid demo
I might have written a pretty confusing title but my question is rather simple.
I'm looking for an efficient way to remove an item from an array. But my array is full objects that has been stringified (I'm writing my app on Node.js and I'm using JSON.stringify method). So my array is like this;
"{\"userID\":\"15\",
\"possibleFollowers\":[
{\"followerID\":\"201\",\"friends\":716},
{\"followerID\":\"202\",\"friends\":11887},
{\"followerID\":\"203\",\"friends\":11887}],
\"name\":\"John\",
\"lon\":\"Doe\"}"
My question is on Javascript(or Node). If I wanted to remove the from possibleFollowers with "followerID: 202", how would I be able to do that efficiently?
var string = "…";
var obj = JSON.parse(string);
obj.possibleFollowers = obj.possibleFollowers.filter(function(fol) {
return fol.followerID != "202";
});
string = JSON.stringify(obj);
var data = "{\"userID\":\"15\",\"possibleFollowers\":[{\"followerID\":\"201\",\"friends\":716},{\"followerID\":\"202\",\"friends\":11887},{\"followerID\":\"203\",\"friends\":11887}],\"name\":\"John\",\"lon\":\"Doe\"}";
var dataObject = JSON.parse(data);
dataObject.possibleFollowers = dataObject.possibleFollowers.filter(function(follower) {
return !(follower.followerID == "202");
});
data = JSON.stringify(dataObject);
In javascript, the splice method is used to delete an array element by index.
see :
http://www.roseindia.net/java/javascript-array/javascript-array-remove-index.shtml
try just to delete it by using "delete"
for (var i in possibleFollowers) {
if (possibleFollowers[i]['followerId'] == '216') {
delete possibleFollowers[i];
}
}
I have this array:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
now i want to remove the line, lets say, where a=5. So afterwards the array looks like this:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"}]
How do i do this the easiest and fastest way?
You can use jQuery.map which allows you to return null for an element to be deleted.
eg:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
var newArray = $.map(array, function(e){
return (e.a == "5") ? null : e;
});
// newArray contains [{"a":"1","b":"2"},{"a":"3","b":"1"}]
Live example (watch the console): http://jsfiddle.net/2Yz7f/
Javascript (non jQuery) approach: http://jsfiddle.net/VYKBc/
Maybe this is your answer
array.splice(2,1);
HI, i have an object: var myobject = new Object;
and i want to dynamically fill it with properties while looping through jquery input collection in that manner:
$('.test').each(function(){
myobject.$(this).attr('name') = $(this).val();
});
what i'm doing wrong here?
thanks in advance
Try this:
$('.test').each(function () {
var e = $(this);
myobject[e.attr('name')] = e.val();
});
Objects in JavaScript can be accessed using object.property or object['property'] (these two are equivalent). The latter allows you to use expressions (like variables): object[propertyName].
With the way you are doing it:
var myObject = {};
$('.test').each(
function(){
var elem = $(this);
myObject[elem.attr('name')] = elem.val();
}
);