如果“stock_status”为false,尝试遍历此树(我认为这是最初的对象)来控制记录“products”键。从来没有做过这样的事情。
var m = {
"option_labels": {
"jamocha": {
"configurable_product": {
"small_image": null,
"base_image": null
},
"products": ["12"],
"stock_status": false
},
"matte white": {
"configurable_product": {
"small_image": null,
"base_image": null
},
"products": ["13"],
"stock_status": true
},
"cool gray": {
"configurable_product": {
"small_image": null,
"base_image": null
},
"products": ["146"],
"stock_status": true
}
}
};
答案 0 :(得分:0)
您可以使用Object.keys()
和forEach
循环来检查stock_status
var m = {"option_labels":{"jamocha":{"configurable_product":{"small_image":null,"base_image":null},"products":["12"],"stock_status":false},"matte white":{"configurable_product":{"small_image":null,"base_image":null},"products":["13"],"stock_status":true},"cool gray":{"configurable_product":{"small_image":null,"base_image":null},"products":["146"],"stock_status":true},"princess pink":{"configurable_product":{"small_image":null,"base_image":null},"products":["147"],"stock_status":true},"key lime":{"configurable_product":{"small_image":null,"base_image":null},"products":["148"],"stock_status":true},"nautical blue":{"configurable_product":{"small_image":null,"base_image":null},"products":["149"],"stock_status":true},"slate":{"configurable_product":{"small_image":null,"base_image":null},"products":["294"],"stock_status":true}}}
Object.keys(m.option_labels).forEach(function(e) {
var option = m.option_labels[e];
if(option.stock_status == false) console.log(option.products)
})

如果您只想返回数字而不是数组,可以将console.log(option.products)
更改为Number(option.products[0])
var m = {"option_labels":{"jamocha":{"configurable_product":{"small_image":null,"base_image":null},"products":["12"],"stock_status":false},"matte white":{"configurable_product":{"small_image":null,"base_image":null},"products":["13"],"stock_status":true},"cool gray":{"configurable_product":{"small_image":null,"base_image":null},"products":["146"],"stock_status":true},"princess pink":{"configurable_product":{"small_image":null,"base_image":null},"products":["147"],"stock_status":true},"key lime":{"configurable_product":{"small_image":null,"base_image":null},"products":["148"],"stock_status":true},"nautical blue":{"configurable_product":{"small_image":null,"base_image":null},"products":["149"],"stock_status":true},"slate":{"configurable_product":{"small_image":null,"base_image":null},"products":["294"],"stock_status":true}}}
Object.keys(m.option_labels).forEach(function(e) {
var option = m.option_labels[e];
if(option.stock_status == false) console.log(Number(option.products[0]))
})

答案 1 :(得分:0)
您可以使用两个嵌套循环作为对象的键并合并结果。
var m = { "option_labels": { "jamocha": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["12"], "stock_status": false }, "matte white": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["13"], "stock_status": true }, "cool gray": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["146"], "stock_status": true }, "princess pink": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["147"], "stock_status": true }, "key lime": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["148"], "stock_status": true }, "nautical blue": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["149"], "stock_status": true }, "slate": { "configurable_product": { "small_image": null, "base_image": null }, "products": ["294"], "stock_status": true } } },
products = [];
Object.keys(m).forEach(function (k) {
Object.keys(m[k]).forEach(function (kk) {
if (!m[k][kk].stock_status) {
products = products.concat(m[k][kk].products);
}
});
});
console.log(products);

答案 2 :(得分:0)
在这里,您可以使用lodash方法this answered question迭代对象和方法_.forOwnRight(object, [iteratee=_.identity])以获取连接数组结果:
let m = {"option_labels":{"jamocha":{"configurable_product":{"small_image":null,"base_image":null},"products":["12"],"stock_status":false},"matte white":{"configurable_product":{"small_image":null,"base_image":null},"products":["13"],"stock_status":true},"cool gray":{"configurable_product":{"small_image":null,"base_image":null},"products":["146"],"stock_status":true},"princess pink":{"configurable_product":{"small_image":null,"base_image":null},"products":["147"],"stock_status":true},"key lime":{"configurable_product":{"small_image":null,"base_image":null},"products":["148"],"stock_status":true},"nautical blue":{"configurable_product":{"small_image":null,"base_image":null},"products":["149"],"stock_status":true},"slate":{"configurable_product":{"small_image":null,"base_image":null},"products":["294"],"stock_status":true}}},
result = [];
_.forOwnRight(m.option_labels, (v, k) => {
if (!v.stock_status) {
result = _.concat(result, v.products);
}
});
// array result
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>