获取 jQuery 模板数组中的第一项
Grab the first item in jQuery Template array
我有一个数组 sizes
,我可以在我的 jQuery 模板中访问它。但是,在某些情况下,数组只有一个项目,我想直接访问它。如何使用 jQuery 模板执行此操作?
此尝试无效:
<input type="hidden" name="product_id" data-related-product-id="${$index}" value="${sizes[0]}${product_id}" />
并输出:
<input type="hidden" value="[object Object]{product_id}" data-related-product-id="1" name="product_id">
我似乎找不到有关如何执行此操作的任何文档。
这是 sizes
上 JSON.stringify
的输出。
[{"product_id":"51507322000","free_stock":"0","Size":"000xs","title":"XS","sizes_group_id":"29,33","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"29":{"id":"29","title":"XS","position":"20","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322001","free_stock":"4","Size":"001s","title":"S","sizes_group_id":"30,34","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"30":{"id":"30","title":"S","position":"21","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322002","free_stock":"4","Size":"002m","title":"M","sizes_group_id":"31,36","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"31":{"id":"31","title":"M","position":"22","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322003","free_stock":"9","Size":"003l","title":"L","sizes_group_id":"32,38","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"32":{"id":"32","title":"L","position":"23","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322004","free_stock":"4","Size":"004xl","title":"XL","sizes_group_id":"35,40","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"35":{"id":"35","title":"XL","position":"24","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322005","free_stock":"2","Size":"005xxl","title":"XXL","sizes_group_id":"46","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"46":{"id":"46","title":"XXL","position":"25","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"}]
基于size
数组结构,你将不得不做这样的事情:
${sizes['somethingHere'][0]}
.
您可以使用 JSON.stringify(sizes)
转储并查看 [object Object]
包含的内容。
编辑:
将代码中的 ${sizes[0]}${product_id}
替换为 ${sizes[0]['product_id']}
。这应该让你成为数组中的第一个元素(如果只有一个元素,则为唯一元素)。
使用 ${sizes[0]['product_id']}
,这将为您提供产品数组中第一个产品的产品 ID。
很高兴,您的问题已解决。
快乐编码:-)
我有一个数组 sizes
,我可以在我的 jQuery 模板中访问它。但是,在某些情况下,数组只有一个项目,我想直接访问它。如何使用 jQuery 模板执行此操作?
此尝试无效:
<input type="hidden" name="product_id" data-related-product-id="${$index}" value="${sizes[0]}${product_id}" />
并输出:
<input type="hidden" value="[object Object]{product_id}" data-related-product-id="1" name="product_id">
我似乎找不到有关如何执行此操作的任何文档。
这是 sizes
上 JSON.stringify
的输出。
[{"product_id":"51507322000","free_stock":"0","Size":"000xs","title":"XS","sizes_group_id":"29,33","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"29":{"id":"29","title":"XS","position":"20","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322001","free_stock":"4","Size":"001s","title":"S","sizes_group_id":"30,34","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"30":{"id":"30","title":"S","position":"21","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322002","free_stock":"4","Size":"002m","title":"M","sizes_group_id":"31,36","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"31":{"id":"31","title":"M","position":"22","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322003","free_stock":"9","Size":"003l","title":"L","sizes_group_id":"32,38","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"32":{"id":"32","title":"L","position":"23","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322004","free_stock":"4","Size":"004xl","title":"XL","sizes_group_id":"35,40","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"35":{"id":"35","title":"XL","position":"24","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"},{"product_id":"51507322005","free_stock":"2","Size":"005xxl","title":"XXL","sizes_group_id":"46","oos_status":"0","Dept":"51","slug":"this-is-the-slug","sizes_group":{"46":{"id":"46","title":"XXL","position":"25","type_title":"Clothing","depts_to_include":",01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,","depts_to_exclude":",81,85,"}},"product_url":"https://crmpicco.dev/p/this-is-the-slug/"}]
基于size
数组结构,你将不得不做这样的事情:
${sizes['somethingHere'][0]}
.
您可以使用 JSON.stringify(sizes)
转储并查看 [object Object]
包含的内容。
编辑:
将代码中的 ${sizes[0]}${product_id}
替换为 ${sizes[0]['product_id']}
。这应该让你成为数组中的第一个元素(如果只有一个元素,则为唯一元素)。
使用 ${sizes[0]['product_id']}
,这将为您提供产品数组中第一个产品的产品 ID。
很高兴,您的问题已解决。
快乐编码:-)