我在这里绞尽脑汁,需要一些快速帮助。如何返回对象,如果它包含JavaScript中的一个字母
我有以下的JSON对象:
"product": {
"diagnostics": null,
"skus": [{
"itemCode": "Q28988",
"salesCode": "Q2898800",
"listPrice": 22,
"salePrice": 22,
"numberOfBottles": 1,
"salePricePerBottle": 22,
"id": "esku580806",
"vppApplier": false,
"vppPrice": null,
"vppDiscountPct": null
}, {
"itemCode": "C28988",
"salesCode": "C2898800",
"listPrice": 264,
"salePrice": 264,
"numberOfBottles": 12,
"salePricePerBottle": 22,
"id": "esku580811",
"vppApplier": false,
"vppPrice": null,
"vppDiscountPct": null
}],
"id": "eprod440905",
}
,我需要过滤的SKU阵列,只返回以字母“Q”开头itemCodes。我使用下划线和JavaScript,这是我能得到的最接近的,但它似乎没有返回任何东西。
var codes = _.filter($scope.cb.recommendations, function(obj){
var startLetter = obj.product.skus.slice();
return startLetter[0] === 'Q';
});
$ scope.cb.recommendations是JSON对象 - 片段在上面。十分感谢!
难道你不想看看“itemCode”字段,而不仅仅是SKU对象的数组吗? – Pointy 2014-09-29 22:30:15
是的,我确实 - 但不知道该怎么做?是吗? obj.product.skus []。itemCode - 但似乎失败 – jrutter 2014-09-29 22:32:13
什么是'$ scope.cb.recommendations'?似乎你想过滤'obj.product.skus'。你希望结果是那些具有以'Q'开头的'itemCode'的sku元素,还是你想要一个项目代码数组? – 2014-09-29 22:32:38