()中的下划线没有按预期工作
我有一些问题使用_.where()。 它没有按预期工作。 这里是我的例子JSON,
var result = [ { image: { id: 158, ig_id: "968545232755317101_1532289565", status: "hide", }, id: 1, month: 4, year: 2015 }, { image: { id: 152, ig_id: "968547282620733844_1532289565", status: "approve", }, id: 2, month: 4, year: 2015 }, ]
我想要获取图像集合的地方='隐藏'。 我喜欢这个,
console.log(_.where(result.image, { status : 'hide'})); // This return [] console.log(_.where(result, { image.status : 'hide'})); // This will error console.log(_.where(result, { 'image.status' : 'hide'})); // This return []
我不知道如何以正确的方式去做。 我真的需要帮助。
谢谢。
你可以用这样的香草JS做到这一点:
console.log(result.filter(function (value) { return value.image.status === 'hide'; }));
我不相信Underscore.js支持在where()
深入比较,但是如果使用lodash,则可以这样做:
console.log(_.where(result, { image: {status : 'hide'}}));