简体   繁体   English

如何基于深度嵌套数组更新 state?

[英]How can I update the state based on a deeply nested array?

I am having difficulty updating an array of products based on stock.我很难根据库存更新一系列产品。 This is the array:这是数组:

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 } ], } ]常量数据= [性别:[{男:真,女:假}],股票:[{尺寸: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}],}]

I want to update the array based on the following criteria:我想根据以下条件更新数组:

1) If the sizes that the client is choosing are not included in the array, I want to return only the product that includes the sizes being chosen. 1)如果客户选择的尺寸不包含在数组中,我只想返回包含所选尺寸的产品。 for this I am updating.为此,我正在更新。

I have the following code to exemplify my goal:我有以下代码来说明我的目标:

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

const array = [];常量数组 = [];

for (const cat of set3) { for (set3 的 const cat) {

 array.push(cat)

} }

I am pushing a set of numbers that the client chose to an array and I want to update the state only if the product includes these sizes (sizes: 4, 5.5, 6, 6.5).我将客户选择的一组数字推送到一个数组,并且我想仅在产品包含这些尺寸(尺寸:4、5.5、6、6.5)时更新 state。

2) I only want to return the product that includes the above criteria. 2) 我只想退回包含上述条件的产品。

This is what I have tried so far:这是我到目前为止所尝试的:

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

const array = [];常量数组 = [];

for (const cat of set3) { array.push(cat) } 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 } }) }) 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 } })

When I console log the item it returns the item that includes what I am looking for but A) it does not update the product and B) when I console log it, it does not actually return the item I am looking for.当我控制台记录该项目时,它会返回包含我正在寻找的项目的项目,但是 A)它不会更新产品和 B)当我控制台记录它时,它实际上并没有返回我正在寻找的项目。 I am sure this has to do with something I don't know about JavaScript.我确信这与我不了解 JavaScript 的事情有关。

You can filter the objects directly based on the selected sizes.您可以根据选定的大小直接过滤对象。 The idea is that a product should be included if it has non-zero stock for all the selected sizes.这个想法是,如果产品的所有选定尺寸的库存均非零,则应包括该产品。 The demo below includes data from your sandbox:下面的演示包含来自您的沙箱的数据:

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

@CodeManiac I am sorry, let me repost this so you can see what I did: @CodeManiac 对不起,让我重新发布一下,这样你就可以看到我做了什么:

const mappedData = Data.map((product) => { const mappedData = Data.map((product) => {

> return product.stock.map((item) => {  
if(item.stock === 0) {  
return
> item.size = false } 
 else {  return item.size } }) }).find((item) => { 
> const check = array.every(r => item.indexOf((r)) >= 0);  const state =
> [];  if( check ) { return item } else { return null } })

Also at this moment, this only returns the current size that does not include the numbers on the set.同样在这一刻,这仅返回不包括集合中数字的当前大小。 How would I return the actual product that matches the criteria?我将如何退回符合条件的实际产品?

I decided to include the code in a sandbox to get more help with this problem.我决定将代码包含在沙箱中以获得更多解决此问题的帮助。 I have not yet been able to solve this but I believe that I am really close:我还没有能够解决这个问题,但我相信我真的很接近:

https://codesandbox.io/s/stoic-kalam-6x3xj https://codesandbox.io/s/stoic-kalam-6x3xj

As you can see in the sandbox, I am successfully getting back the sizes that are in stock based on the array however I am not returning the entire object.正如您在沙箱中看到的那样,我成功地取回了基于数组的库存尺寸,但是我没有返回整个 object。 Would anyone be able to give me any advise on this?任何人都可以给我任何建议吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM