简体   繁体   English

如何从jQuery中的多个选择器创建对象?

[英]How to create objects from multiple selectors in jquery?

I have two elements (#list1, #list2 in html) . 我有两个元素(#list1, #list2 in html) I need to create object and populate it from these two elements. 我需要创建对象并从这两个元素中填充它。 For example: 例如:

$('#list1, #list2').each(function(index,value){ 
  var object =[ {#list1.value, #list2.value} ]; 
})

Something like that. 这样的事情。 So it can add these elements to array with each iteration. 因此,它可以在每次迭代时将这些元素添加到数组中。 How it can be done? 怎么做?

  1. it is .val() 它是.val()
  2. you can push OR you can map 你可以推或者你可以映射
  3. You can use a class so you do not need to list 您可以使用课程,因此无需列出

NOTE: If there is no value attribute on the field, using this.getAttribute("value") on the .get examples will result in any field without value attribute to be omitted from the array instead of adding an empty value (Thanks @adz5A) 注意:如果该字段上没有value属性,则在.get示例上使用this.getAttribute("value")将导致数组中没有value属性的任何字段被忽略,而不是添加一个空值(感谢@ adz5A )

http://jsfiddle.net/mplungjan/jLsa80m9/7/ http://jsfiddle.net/mplungjan/jLsa80m9/7/


 var object1 = []; $('#list1, #list2').each(function() { object1.push($(this).val()) }) console.log(object1); // Smarter: var object2 = $('#list1, #list2') .map(function(index,$list) { return $list.value; // or this.value or $(this).val(); }) .get(); console.log(object2); // EVEN Smarter: var object3 = $('.list') .map(function() { return this.value; }) .get(); console.log(object3); // The two following versions were posted by @rmn - I include them here for // completeness sake. Upvote his answer if you like them // ES6 with jQuery var object4 = $('#list1, #list2').get().map(el => el.value) console.log(object4); // ES6 without jQuery var object5 = [...document.querySelectorAll('#list1, #list2')].map(el => el.value) console.log(object5); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" value="value1" id="list1" class="list" /> <input type="text" value="value2" id="list2" class="list" /> 

I think you are looking for .get() . 我认为您正在寻找.get() It returns as an array the set of matched DOM elements. 它以数组形式返回匹配的DOM元素集。 You can easily combined this with map to retrieve the value of some attribute for instance 您可以轻松地将其与map结合起来,例如检索某些属性的值

$("selector").map(function () { return this.getAttribute("value"); }).get();   

Will get you the value attribute for the selection into an array. 将选择的value属性带入数组。 Note that you can use arrow function inside map as the second argument is the dom node itself, in my example the dom element is bounded to the lexical context this . 请注意,您可以在map内部使用箭头函数,因为第二个参数是dom节点本身,在我的示例中dom元素绑定到词法上下文this

With jQuery 使用jQuery

$('#list1, #list2').get().map(el => el.value)

Without jQuery 没有jQuery

[...document.querySelectorAll('#list1, #list2')].map(el => el.value)

you can create array outside the loop and push it on each iteration 您可以在循环外创建数组并将其推入每次迭代

var object =[];
$('#list1, #list2').each(function(){ 
  object.push($(this).val()); 
});

If you want to have id in your object, this is better: 如果要在对象中包含ID,则更好:

 function getList(){ var list = [] $("#input1, #input2").each(function(){ list.push({id: this.id, value: this.value }); }); console.log(list); /* OUTPUT: [ { "id": "input1", "value": "v1" }, { "id": "input2", "value": "v2" } ] */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="input1" type="text" value="v1" /> <input id="input2" type="text" value="v2" /> <input type="button" onclick="getList()" value="get list" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM