Last post Jan 25, 2019 06:36 AM by Jenifer Jiang
Member
75 Points
513 Posts
Jan 24, 2019 07:09 PM|zhyanadil.it@gmail.com|LINK
var result = [];
$("input[class^='chk_colour']:checked").each(function (index, el) {
var color = $(el).val(); var size = ""; var weight = ""; $("input[class^='chk_size']:checked").each(function (index, el) { size = $(el).val(); $("input[class^='chk_weight']:checked").each(function (index, el) { weight = $(el).val();
}) }); });
i want to write this code
var d = [color, size, weight]
for (var i = 0; i < d.length; i++) { result.push( d[i] ); }
but is not working
instead this line
result.push({ color, size, weight });
Contributor
2150 Points
705 Posts
Jan 25, 2019 06:36 AM|Jenifer Jiang|LINK
Hi zhyanadil.it@gmail.com,
According to your description and code, I suggest that you should define the result array and the push data to it like:
var d = ["color","size", "weight"]; var result = []; for (var i = 0; i < d.length; i++) { result.push(d[i]); } console.log(result);
The type in an array is always number or string.
For more about object to array you could refer to this link: https://stackoverflow.com/questions/7858385/how-to-add-values-to-an-array-of-objects-dynamically-in-javascript
Best Regards,
Jenifer
Member
75 Points
513 Posts
how to put object to the array dynamically
Jan 24, 2019 07:09 PM|zhyanadil.it@gmail.com|LINK
var result = [];
$("input[class^='chk_colour']:checked").each(function (index, el) {
var color = $(el).val();
var size = "";
var weight = "";
$("input[class^='chk_size']:checked").each(function (index, el) {
size = $(el).val();
$("input[class^='chk_weight']:checked").each(function (index, el) {
weight = $(el).val();
})
});
});
i want to write this code
var d = [color, size, weight]
for (var i = 0; i < d.length; i++) {
result.push( d[i] );
}
but is not working
instead this line
result.push({ color, size, weight });
Contributor
2150 Points
705 Posts
Re: how to put object to the array dynamically
Jan 25, 2019 06:36 AM|Jenifer Jiang|LINK
Hi zhyanadil.it@gmail.com,
According to your description and code, I suggest that you should define the result array and the push data to it like:
The type in an array is always number or string.
For more about object to array you could refer to this link: https://stackoverflow.com/questions/7858385/how-to-add-values-to-an-array-of-objects-dynamically-in-javascript
Best Regards,
Jenifer
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.