比较两个数组javascript



我在耐克网站上做木偶。

我得到尺寸和库存并显示为数组,

{ sku: 72961, skuname: 'Talla: 6 - Color: Azul', quantity: 2 },
{ sku: 72962, skuname: 'Talla: 6.5 - Color: Azul', quantity: 4 },
{ sku: 72963, skuname: 'Talla: 7 - Color: Azul', quantity: 5 },
{ sku: 72964, skuname: 'Talla: 7.5 - Color: Azul', quantity: 7 },
{ sku: 72965, skuname: 'Talla: 8 - Color: Azul', quantity: 6 },
{ sku: 72966, skuname: 'Talla: 8.5 - Color: Azul', quantity: 5 },
{ sku: 72967, skuname: 'Talla: 9 - Color: Azul', quantity: 8 },
{ sku: 72968, skuname: 'Talla: 9.5 - Color: Azul', quantity: 8 },
{ sku: 72969, skuname: 'Talla: 10 - Color: Azul', quantity: 6 },
{ sku: 72970, skuname: 'Talla: 10.5 - Color: Azul', quantity: 4 },
{ sku: 72971, skuname: 'Talla: 11 - Color: Azul', quantity: 1 },
{ sku: 72972, skuname: 'Talla: 11.5 - Color: Azul', quantity: 2 }
]

每10秒刷新一次所以我想找到一种方法,将最后一个数组与前一个数组进行比较,以知道是否添加了新的Size或quantity

我的代码

const pagina = "https://nike.cl"
const producto = "/dm0121-400-nike-dunk-low-retro-qs/p"

const page = await browser.newPage();
await page.setViewport({ width: 1440, height: 900});

while (true) {
await page.goto(pagina + producto, { waitUntil: 'networkidle2' },);
var SKU = await page.evaluate("skuJson_0")
let filter = SKU.skus.filter( d => d.availablequantity > 0)
var SKUfiltered = filter.map(function(d){
return{
sku: d.sku,
skuname: d.skuname,
quantity: d.availablequantity
}})
console.log(SKUfiltered)
await page.waitForTimeout(10000)

我认为最好的解决方案是存储SKU数据来检查以前是否存在以及值是否相等。

如果该值通过了这两项检查,您可以将其存储在您的"SKU register"中。

下一步,您可以进行其他检查来过滤值。在你的情况下,只检查数量是否为零。

作为补充,如果你有很多SKU和存储一个巨大的可能你达到内存限制。如果数组太大,你可以删除它。要做到这一点,您可以创建一个Array来按顺序存储值,并在达到某些限制(如1000个元素)时删除。

在下一个示例中,我模拟每次执行getSKU时获取数据。您必须使代码适应您的解决方案:

// == FAKE DATA ==
let res_i = 0;
const res_data = [
[
{ sku: 1, skuname: "1", quantity: 0 }, // empty
{ sku: 2, skuname: "2", quantity: 1 },
],
[
{ sku: 1, skuname: "1", quantity: 5 },
{ sku: 2, skuname: "2", quantity: 1 }, // same quantity
{ sku: 3, skuname: "3", quantity: 0 }, // empty
],
[
{ sku: 1, skuname: "1", quantity: 0 }, // empty
{ sku: 3, skuname: "3", quantity: 3 },
{ sku: 4, skuname: "4", quantity: 3 },
],
];
// == END FAKE DATA ==
// An array to save the lastest SKU and a Map to store important values
const SKURegLog = [];
const SKURegdata = new Map();
// A funtion to simulate the data
function getSKU() {
const SKU = res_data[res_i++]; // Simulate the incoming data
return SKU.filter((v) => {
// Get the current SKU in the register
const curSKU = SKURegdata.get(v.sku);
// If exists and the quantity is the same, discard
if (curSKU !== undefined && curSKU.quantity === v.quantity) {
return false;
}
// If the SKU not exists in register add to the array
if (curSKU === undefined) {
SKURegLog.push(v.sku);
}
// Include the SKU in the register. If the array reach
// the limit (1000) delete the olders
SKURegdata.set(v.sku, {quantity: v.quantity});
if (SKURegLog.length > 1000) {
const oldSKU = SKURegLog.shift();
SKURegdata.delete(oldSKU);
}
// Check the current quantity
if (v.quantity === 0) {
return false;
}
// Include the SKU
return true;
});
}
console.log(getSKU());
console.log(getSKU());
console.log(getSKU());
console.log("= Register data:");
console.log(SKURegLog);
console.log(SKURegdata);

输出:

1: [ { sku: 2, skuname: '2', quantity: 1 } ]
2: [ { sku: 1, skuname: '1', quantity: 5 } ]
3: [ { sku: 3, skuname: '3', quantity: 3 }, { sku: 4, skuname: '4', quantity: 3 }]
== Register data:
[ 1, 2, 3, 4 ]
Map(4) {
1 => { quantity: 0 },
2 => { quantity: 1 },
3 => { quantity: 3 },
4 => { quantity: 3 }
}

相关内容

  • 没有找到相关文章

最新更新