I am trying to sort all objects that match the regex into an array. This does not seem to work with the spread operator and useState, is there any way I can do that? The result I am getting now is the samples thing only gives me the last object that matches it and nothing else. The desired effect I want is all the samples that match get pushed into the samples state.
const [accessories, setAccessories] = useState([]);
const [paints, setPaints] = useState([]);
const [samples, setSamples] = useState([]);
// Load order into state
useEffect(() => {
loadUser();
getOrderById(match.params.orderId);
}, []);
// Load order into state
useEffect(() => {
if (!loading) {
console.log(order.line_items);
for (let i = 0; i < order.line_items.length; i++) {
if (order.line_items[i].sku.match(/^(TAC|T.BU.AC)/)) {
console.log('SKU: ', order.line_items[i].sku);
//@ts-ignore
setAccessories([...accessories, order.line_items[i]]);
console.log(accessories);
}
if (order.line_items[i].sku.startsWith('TBA') || order.line_items[i].sku.match(/^TCR(?!0000)/)
|| order.line_items[i].sku.match(/^TCR0000/)) {
//@ts-ignore
setPaints([...paints, order.line_items[i]]);
}
if (order.line_items[i].sku.match(/^TCR\d+P?\d+SAMP/)) {
console.log(samples);
console.log(order.line_items[i]);
//@ts-ignore
setSamples([...samples, ...[order.line_items[i]]]);
}
}
}
}, [loading]);
order.line_items[i].sku.match(/^TCR(?!0000)/) || order.line_items[i].sku.match(/^TCR0000/)does not make sense - if theskustarts with TCR, at least one of those will evaluate totrue. What's the logic you're looking for relating to 0000 there?