gpt4 book ai didi

javascript - 如何根据深度嵌套数组更新状态?

转载 作者:行者123 更新时间:2023-11-29 15:07:33 24 4
gpt4 key购买 nike

我很难根据库存更新一系列产品。这是数组:

const Data = [ gender: [{ male: true, female: false }], 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 } ] }, { gender: [{male: false, female: true }], 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 } ], } ]

我想根据以下条件更新数组:

1) 如果客户选择的尺寸不包含在数组中,我只想返回包含所选尺寸的产品。为此,我正在更新。

我有以下代码来举例说明我的目标:

var set3 = new Set([ 4, 5.5, 6, 6.5 ]);

const array = [];

for (const cat of set3) {

array.push(cat)

}

我将客户选择的一组数字推送到一个数组中,并且我只想在产品包含这些尺寸(尺寸:4、5.5、6、6.5)时更新状态。

2) 我只想退回符合上述条件的产品。

这是我到目前为止尝试过的:

var set3 = new Set([ 4, 5.5, 6, 6.5 ]);

const array = [];

for (const cat of set3) { array.push(cat) }

let mappedData = Data.map((product) => { return product.stock.map((item) => { if(item.stock === 0) { return item.size = undefined } else { return item.size } }) })

mappedData.forEach((item, idx, array) => {check = array.every (r => item.indexOf(r) >= 0)
if( check ) {
console.log(item)
}
else {
return item
} })

当我控制台记录项目时,它返回包含我正在寻找的项目的项目,但 A) 它不更新产品,B) 当我控制台记录它时,它实际上并没有返回我正在寻找的项目。我确信这与我对 JavaScript 不了解的东西有关。

最佳答案

您可以直接根据所选尺寸过滤对象。这个想法是,如果所有选定尺寸的产品都具有非零库存,则应该包括该产品。下面的演示包括来自沙箱的数据:

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);

关于javascript - 如何根据深度嵌套数组更新状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com