See https://api.jquery.com/toArray/
$( "li" ).toArray()
This works great in cases like
$( "li" ).toArray().each( function(li) {
console.log('do something with this list item', li);
})
or if you really want
$( "li" ).toArray()[0]
Answer from Paul Schwarz on Stack OverflowSee https://api.jquery.com/toArray/
$( "li" ).toArray()
This works great in cases like
$( "li" ).toArray().each( function(li) {
console.log('do something with this list item', li);
})
or if you really want
$( "li" ).toArray()[0]
Demo
var divs = $('#elements div');
var links = $('#elements div a');
- If you want the DOM elements, then you can access them with the array style like
divs[0]orlinks[2]. - If you want to get the specific jQuery object, you can access them with
divs.eq(0)orlinks.eq(1).
var array = $('li').map(function(){
return $.trim($(this).text());
}).get();
http://api.jquery.com/map/
Demo Fiddle --> http://jsfiddle.net/EC4yq/
Try this:
$("li").map(function(){ return this.innerText })
or this:
$("li").toArray().map(function(i){ return i.innerText })
Use map():
var myArray = $("input:checkbox:checked").map(function(){
return this.getAttribute("category_uid");
}).get();
As bpierre suggested, use .map(). His answer is correct.
If you need this behavior for different attributes, you might as well write is as a reusable function (“jQuery plugin”):
jQuery.fn.pluck = function(attr) {
return this.map(function() {
return this.getAttribute(attr);
}).get();
};
$('input:checkbox:checked').pluck('category_uid'); // ["1.3", "1.3.1"]
P.S. category_uid is not a valid attribute in HTML. Consider using custom data-* attributes instead, e.g. data-category-uid="foo".
var optionTexts = [];
$("ul li").each(function() { optionTexts.push($(this).text()) });
...should do the trick. To get the final output you're looking for, join() plus some concatenation will do nicely:
var quotedCSV = '"' + optionTexts.join('", "') + '"';
Without redundant intermediate arrays:
var arr = $('li').map(function(i,el) {
return $(el).text();
}).get();
See jsfiddle demo
Your selector:
$("input[id^="author"]")
is incorrect, because you're trying to use double quotes inside double quotes. One of the pairs needs to be single quotes.
If you were using a JS debugger this should be immediately visible.
FWIW, this is a cleaner way of retrieving the required array:
var authors = $('input[id^="author"]').map(function() {
return this.value;
}).get();
Your "html array" is not an array at all, even assuming there are a number of other similar inputs (though when the form is submitted your chosen server-side language likely lets you access the values of inputs with the same name as an array). Anyway...
The main problem is that your JS has a syntax error:
$("input[id^="author"]")
If you want to include double-quotes inside a double-quoted string literal you have to do escape them:
$("input[id^=\"author\"]")
Or quote the string with singles:
$('input[id^="author"]')
It may be more efficient to avoid the attribute starts-with selector with the id attribute and select by the name attribute, noting that for jQuery the square brackets in the atttribute name need to be escaped with backslashes, and for JS string literals backslashes also need to be escaped:
$('input[name="authors\\[\\]"]')
Also to get the value of an input with jQuery use .val() rather than .text(), but it is more efficient to just use this.value:
var authors = [];
$('input[id^="author"]').each(function(){ authors.push(this.value); });
I'd also recommend declaring arrays with = [] rather than = new Array().
You can use the $.map function from jQuery :
var value = $.map(example, function () {
return this.name;
});
This will return an array of the items.
You can iterate through the object keys and save the name in the array using push:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames);//prints ["someone1", "someone2"]
After @Felix suggestion(with which I agree) there is no need to use Object.keys:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
example.forEach(function(item) {
//get the value of name
var val = item.name
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames); //prints ["someone1", "someone2"]
References
Object.keys()
Array.prototype.forEach()
Array.prototype.push()
Use the starts with selector
$('input[name^="pages_title"]').each(function() {
alert($(this).val());
});
jsfiddle example
Note: In agreement with @epascarello that the better solution is to add a class to the elements and reference that class.
const textValues = $.map($('input[type=text][name="pages_title[]"]'), function(el) { return el.value; });
console.log(textValues);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="Hello" name="pages_title[]">
<input type="text" value="World" name="pages_title[]">
The majority of jQuery methods returns a jQuery object, which can be accessed like it is an array (e.g. it has a .length attribute, elements can be accessed using the square bracket notation ([0]), and it supports some array methods (slice())).
jQuery has a method called toArray() which can be used to convert the jQuery object to a real array.
You can also use get() with no arguments to achieve the same effect (and save you a few key presses).
In future, you can checkout the jQuery API, and the return type for all jQuery methods is listed in the relevant documentation (e.g. for find(), the return type is "jQuery")
If you call .get() on a jQuery object without a parameter, it will return a regular array of DOM elements.
In javascript you can access the elements like this
for(var key in arrayForPrice){
var obj = arrayForPrice[key];
var optionArray = obj.options;
optionArray.forEach(function(data){
// Access the elements are
});
}
var arrayForPrice = {
"93":{"id":"93","code":"color","label":"Color","options":[{"id":"50","label":"Blue","products":["1216","1219","1222"]},{"id":"58","label":"Red","products":["1217","1220","1223"]},{"id":"60","label":"Yellow","products":["1218","1221","1224"]}],"position":"1"},
"141":{"id":"141","code":"size","label":"Size","options":[{"id":"168","label":"S","products":["1216","1217","1218"]},{"id":"169","label":"M","products":["1219","1220","1221"]},{"id":"170","label":"L","products":["1222","1223","1224"]}],"position":"0"}
};
for(var i in arrayForPrice){
console.log(arrayForPrice[i].options);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand