创建值小于 150 的所有产品的数组

var productMaster=[{id:'A441',name:'Pepsi',category:'Soft Drink', price:15, quantity:2},

{id:'B234',name:'Coke', category:'Soft Drink', price:10, quantity:8},

{id:'A617',name:'Mirinda', category:'Soft Drink', price:20, quantity:20},

{id:'B003',name:'Sprite', category:'Soft Drink', price:16, quantity:5},

{id:'B225',name:'Minute Maid', category:'Soft Drink', price:25, quantity:12},

{id:'A742',name:'5Star', category:'Chocloate', price:10, quantity:3},

{id:'B388',name:'Appy', category:'Soft Drink', price:35, quantity:9},

{id:'A899',name:'Gems', category:'Chocloate', price:12, quantity:11},

{id:'B635',name:'KitKat', category:'Chocloate', price:15, quantity:7},

{id:'B408',name:'Perk', category:'Chocloate', price:8, quantity:15},

{id:'A354',name:'Dairy Milk', category:'Chocloate', price:30, quantity:4}];


var arr1 = productMaster.filter(n => n.value=n.price*n.quantity && n.value<150);

console.log(JSON.stringify(arr1));


值定义为价格 * 数量,我得到一个空数组。我必须创建一个包含值小于 150 的所有产品的数组。


Helenr
浏览 124回答 2
2回答

ITMISS

这是您想要实现的目标吗?var productMaster=[{id:'A441',name:'Pepsi',category:'Soft Drink', price:15, quantity:2},{id:'B234',name:'Coke', category:'Soft Drink', price:10, quantity:8},{id:'A617',name:'Mirinda', category:'Soft Drink', price:20, quantity:20},{id:'B003',name:'Sprite', category:'Soft Drink', price:16, quantity:5},{id:'B225',name:'Minute Maid', category:'Soft Drink', price:25, quantity:12},{id:'A742',name:'5Star', category:'Chocloate', price:10, quantity:3},{id:'B388',name:'Appy', category:'Soft Drink', price:35, quantity:9},{id:'A899',name:'Gems', category:'Chocloate', price:12, quantity:11},{id:'B635',name:'KitKat', category:'Chocloate', price:15, quantity:7},{id:'B408',name:'Perk', category:'Chocloate', price:8, quantity:15},{id:'A354',name:'Dairy Milk', category:'Chocloate', price:30, quantity:4}]&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;var arr1 = productMaster.filter(n => n.price*n.quantity < 150);console.log(JSON.stringify(arr1));

婷婷同学_

如果你确实需要将值设置为数量乘以价格,然后获取那些值低于 150 的值,你可以使用map()then filter():var productMaster = [&nbsp; {id:'A441',name:'Pepsi',category:'Soft Drink', price:15, quantity:2},&nbsp; {id:'B234',name:'Coke', category:'Soft Drink', price:10, quantity:8},&nbsp; {id:'A617',name:'Mirinda', category:'Soft Drink', price:20, quantity:20},&nbsp; {id:'B003',name:'Sprite', category:'Soft Drink', price:16, quantity:5},&nbsp; {id:'B225',name:'Minute Maid', category:'Soft Drink', price:25, quantity:12},&nbsp; {id:'A742',name:'5Star', category:'Chocloate', price:10, quantity:3},&nbsp; {id:'B388',name:'Appy', category:'Soft Drink', price:35, quantity:9},&nbsp; {id:'A899',name:'Gems', category:'Chocloate', price:12, quantity:11},&nbsp; {id:'B635',name:'KitKat', category:'Chocloate', price:15, quantity:7},&nbsp; {id:'B408',name:'Perk', category:'Chocloate', price:8, quantity:15},&nbsp; {id:'A354',name:'Dairy Milk', category:'Chocloate', price:30, quantity:4}];var arr1 = productMaster.map((n) => {&nbsp; n.value = n.price * n.quantity;&nbsp; return n;}).filter(n => n.value < 150);console.log(JSON.stringify(arr1));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript