我很难根据库存更新一系列产品。这是一个数组:
const Data =[性别:{男性:真,女性:假},股票:{大小: 3,股票: 100 },{大小: 3.5,股票: 10 },{大小: 4,股票:0 },{大小: 4.5,股票: 330 },{大小: 5,股票:5 },{大小: 5.5,股票: 555 },{大小: 6,股票:6 },{大小: 6.5,股票: 63 },{大小: 7,股票:0 },{大小: 7.5,股票: 100 },{大小: 8,股票: 33 },{大小: 8.5,股票: 333 },{大小: 9,股票: 222 },{大小: 9.5,股票: 99 },{大小: 10,股票: 99 },{大小: 10.5,股票: 77 },{大小: 11,股票: 55 },{性别:{男性:假,女性:真},股票:{大小: 3,股票: 140 },{大小: 3.5,股票: 130 },{大小: 4,股票: 10 },{大小: 4.5,股票: 30 },{大小: 5,股票: 53 },{大小: 5.5,股票: 55 },{大小: 6,股票: 64 },{大小: 6.5,股票: 643 },{大小: 7,股票:0 },{大小: 7.5,股票: 100 },{大小: 8,股票: 334 },{大小: 8.5,股票: 333 },{大小: 9,股票: 22 },{大小: 9.5,股票:9 },{大小: 10,股票: 99 },{大小: 10.5,股票:7 },{大小: 11,股票: 45 },}]
我想根据以下条件更新数组:
1)如果客户端选择的大小不包括在数组中,我只想返回包含所选大小的产品。为此,我正在更新。
我有以下代码来举例说明我的目标:
var set3 =新集( 4,5.5,6,6.5 );
const数组= [];
(set3的康斯特猫){
array.push(cat)
}
我正在将客户端选择的一组数字推送到数组中,只有当产品包含这些大小(大小: 4、5.5、6、6.5)时,我才想更新状态。
2)我只想返回包含上述标准的产品。
这就是我迄今尝试过的:
var set3 =新集( 4,5.5,6,6.5 );
const数组= [];
(set3的康斯特猫){ array.push( cat ) }
设mappedData =Data.map((乘积) => {返回product.stock.map((项目) => { if(item.stock === 0) {返回item.size =未定义}product.stock.map{返回item.size } }) )
mappedData.forEach(( item,idx,array) => {check = array.every (r => item.indexOf(r) >= 0) if( check ){ console.log(item) }=>{ return } })
当我控制台记录该项目时,它返回包含我正在寻找的内容的项,但是它不更新产品,当我记录它时,它实际上不会返回我想要的项目。我确信这与我对JavaScript不了解的事情有关。
发布于 2019-10-18 03:27:52
可以根据选定的大小直接筛选对象。这个想法是,如果一个产品对所有选定的尺寸都有非零库存的话,它就应该包括在内。下面的演示包括沙箱中的数据:
const Data = [{"title":"Scotts","alternative_title":"Jeremy Scotts Pandabear","routeName":"Scotts","gender":[{"male":true,"female":true}],"brand":"Adidas","price":400,"stock":[{"size":3,"stock":100},{"size":3.5,"stock":10},{"size":4,"stock":0},{"size":4.5,"stock":330},{"size":5,"stock":5},{"size":5.5,"stock":555},{"size":6,"stock":6},{"size":6.5,"stock":63},{"size":7,"stock":0},{"size":7.5,"stock":100},{"size":8,"stock":33},{"size":8.5,"stock":333},{"size":9,"stock":222},{"size":9.5,"stock":99},{"size":10,"stock":99},{"size":10.5,"stock":77},{"size":11,"stock":55}],"description":"The Womens Air Jordan 1 Satin Black Toe” is a special construction of the original colorway of the Jordan 1 with satin paneling on the heel. Following the same blueprint of the Satin Shattered Backboard” colorway that was also a women’s exclusive, this Satin Black Toe” edition features a black and white leather upper with red satin at the heel. The Air Jordan wings” logo on the ankle is presented in a metal medallion to complete the premium look. The Women’s Air Jordan 1 Satin Black Toe” released on August 17, 2019 in limited quantities.","image":"https://drive.google.com/uc?id=1Uzaa15R2zn-YAozyyVi7RI5GAlhB0uCo"},{"title":"Boost 350 White","alternative_title":"Yeezy Boost 350 White","routeName":"yeezy","gender":[{"male":true,"female":false}],"brand":"Nike","price":200,"stock":[{"size":3,"stock":140},{"size":3.5,"stock":130},{"size":4,"stock":10},{"size":4.5,"stock":30},{"size":5,"stock":53},{"size":5.5,"stock":55},{"size":6,"stock":64},{"size":6.5,"stock":643},{"size":7,"stock":0},{"size":7.5,"stock":100},{"size":8,"stock":334},{"size":8.5,"stock":333},{"size":9,"stock":22},{"size":9.5,"stock":9},{"size":10,"stock":99},{"size":10.5,"stock":7},{"size":11,"stock":45}],"description":"The adidas Yeezy Boost 350 v2 Static released in December 2018, bringing a new look to Kanye Wests popular silhouette. The updated Yeezy 350 features a transparent mesh panel in place of the usual side stripe. The panel is woven into the Primeknit upper on each lateral side. The Yeezy 350 Static introduced an intricate new Primeknit pattern in white and grey hues. The clean appearance is solidified with a translucent white rubber midsole and outsole that wrap full-length Boost cushioning. The unique design of the adidas Yeezy Boost 350 v2 Static provided a refreshing update that will be in-demand for years to come.","image":"https://drive.google.com/uc?id=16Js6C1UyksZdVrPj_VdVKC16yYQcbMIp"},{"title":"Grey Yeezys","alternative_title":"Adidas Yeezy 500 Bone White","routeName":"yeezy-bone-whit","gender":[{"male":true,"female":false}],"brand":"Dolce & Gabbana","price":350,"stock":[{"size":3,"stock":140},{"size":3.5,"stock":10},{"size":4,"stock":1},{"size":4.5,"stock":340},{"size":5,"stock":53},{"size":5.5,"stock":55},{"size":6,"stock":654},{"size":6.5,"stock":43},{"size":7,"stock":90},{"size":7.5,"stock":70},{"size":8,"stock":34},{"size":8.5,"stock":33},{"size":9,"stock":2},{"size":9.5,"stock":94},{"size":10,"stock":9},{"size":10.5,"stock":0},{"size":11,"stock":0}]}]
const set3 = new Set([11, 10.5]);
const selectedSizes = [...set3];
const filteredProducts = Data.filter(product =>
selectedSizes.every(size =>
product.stock.some(s => s.stock > 0 && s.size === size)));
console.log(filteredProducts);
https://stackoverflow.com/questions/58423829
复制相似问题