从javascript中的数组/对象获取特定值
在这里完全被难住了 我的目标如下:从javascript中的数组/对象获取特定值,javascript,Javascript,在这里完全被难住了 我的目标如下: var stStatus = new StockStatus({ "3":{ "is_in_stock":false, "custom_status_icon":"", "custom_status":"Out of Stock", "product_id":"9", "stockalert":"" }, "5":{ "is_in_stock":true, "custom_status_ico
var stStatus = new StockStatus({
"3":{
"is_in_stock":false,
"custom_status_icon":"",
"custom_status":"Out of Stock",
"product_id":"9",
"stockalert":""
},
"5":{
"is_in_stock":true,
"custom_status_icon":"",
"custom_status":"",
"product_id":"10"
},
"88":{
"is_in_stock":true,
"custom_status_icon":"",
"custom_status":"",
"product_id":"296"
}
});
这是动态创建的,所以我不知道前面的3、5或88个数字。我想知道如何得到3,5,88和他们的平手是库存和产品id
试图让它基本上只进入控制台。记录产品id和3,5,88个数字,如果库存是假的
完全被难住了。您可以使用获取数组中的所有键
然后使用循环遍历数组并将键与对象匹配
var m ={
"3":{
"is_in_stock":false,
"custom_status_icon":"",
"custom_status":"Out of Stock",
"product_id":"9",
"stockalert":""
},
"5":{
"is_in_stock":true,
"custom_status_icon":"",
"custom_status":"",
"product_id":"10"
},
"88":{
"is_in_stock":true,
"custom_status_icon":"",
"custom_status":"",
"product_id":"296"
}
}
var _keyArray = Object.keys(m)
_keyArray.forEach(function(item){
document.write('<pre>'+(m[item]['is_in_stock'])+'</pre>')
var m={
"3":{
“是否有库存”:假,
“自定义状态图标”:“”,
“定制状态”:“缺货”,
“产品id”:“9”,
“stockalert”:”
},
"5":{
“是否有库存”:正确,
“自定义状态图标”:“”,
“自定义状态”:“,
“产品标识”:“10”
},
"88":{
“是否有库存”:正确,
“自定义状态图标”:“”,
“自定义状态”:“,
“产品标识”:“296”
}
}
var\u keyArray=Object.keys(m)
_keyArray.forEach(函数(项){
文件。书写(“”+(m[项目][“库存中])+“”)
您可以在
对象.键上迭代
stStatus[keys[0]]
应该为您提供一个数组,其中包含此实例中的所有键。然后,您可以按如下方式使用结果:
访问第一个字段;与其他字段类似。是什么使得StockStatus
?到目前为止你做了什么?你在使用strStatus
?StockStatus
做什么?你在找对象。键
?一些奇怪的Magento东西我从来都不明白在变量名前面加下划线的吸引力。为什么你用它吗?(你没有错,我只是不明白)变量名中的下划线,document.write
,jsFiddle…还有堆栈片段,它们支持console.log
@evolutionxbox这也是为了表示一个私有变量,请检查这一点,它工作得很好。我这里有一个分叉,我几乎得到了我需要的东西。但似乎无法记录/写入它们的第一个数字n包含在..@Octoxan中不确定您所说的第一个数字是什么意思。我假设您指的是3,5 88等键。如果是,请检查此项
stStatus[keys[0]]