拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 For回圈不迭代变量

For回圈不迭代变量

白鹭 - 2022-01-23 2116 0 0

我只是在学习 javascript,我正在尝试通过 GAS 更新 woocommerce 产品。

有问题的问题如下:

我有一个变量可以决议来自 woocommerce 的回应

for (let sku of skuSearch) {
    var surl = website   "/wp-json/wc/v3/products?consumer_key="   ck   "&consumer_secret="   cs   "&sku="   sku;
    var url = surl
    Logger.log(url)

    var result = UrlFetchApp.fetch(url, optionsGet);
    if (result.getResponseCode() == 200) {

        var wooProducts = JSON.parse(result.getContentText());
        Logger.log(result.getContentText());

    }

然后我有另一个 for 迭代并从包含 wooProducts 的 id sku 和来自不同变量的 price 的新阵列进行迭代,该变量从我的作业表中获取更新的价格:

var idLength = wooProducts.length;
    Logger.log(idLength);
    for (var i = 0; i < idLength; i  ) {
        var container = [];
        Logger.log(i);
        container.push({
            id: wooProducts[i]["id"],
            sku: wooProducts[i]["sku"],
            price: data[i]["price"],
        });

我不能确切地说出为什么它不起作用。我的意思是 for 回圈有效,它在每个回圈中推送 id、sku 和 price,只是 data[i] 只提供第一个 ? 物件?而不是像 wooProducts 那样回圈,在每个回圈中添加 1。

我将复制 3 个回圈,因此它非常清晰,我不确定它是否已经清晰。

回圈 1:

[{"id":1622,"sku":"PD-1000-B","price":8145.9}]

回圈 2:

[{"id":1624,"sku":"PD-1007-A","price":8145.9}]

回圈 3:

[{"id":1625,"sku":"PD-1014","price":8145.9}]

如您所见,id sku 发生了变化,但价格没有变化。

对于进一步的背景关系,我将包括在 For 之外宣告的资料变量:

const data = codigos.map(function(codigos, indice) {
    return {
        sku: codigos[0],
        price: precios[indice][0]
    }
})

//** 编辑:

我正在添加整个代码,所以它可能更有意义吗?

function getDataloopwoo() {
var ck = 'xxx'

var cs = 'xxx'

var website = 'xxx'

var optionsGet =

    {
        "method": "GET",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "muteHttpExceptions": true,

    };    

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PreciosBULK');
var codigos = sheet.getRange("A2:A").getValues();
var precios = sheet.getRange("B2:B").getValues();
var skuSearch = sheet.getRange("A2:A").getValues();

const data = codigos.map(function(codigos, indice) {
    return {
        sku: codigos[0],
        price: precios[indice][0]
    }
})
Logger.log(skuSearch)

for (let sku of skuSearch) {
    var surl = website   "/wp-json/wc/v3/products?consumer_key="   ck   "&consumer_secret="   cs   "&sku="   sku;
    var url = surl
    Logger.log(url)

    var result = UrlFetchApp.fetch(url, optionsGet);
    if (result.getResponseCode() == 200) {

        var wooProducts = JSON.parse(result.getContentText());
        Logger.log(result.getContentText());

    }
    var idLength = wooProducts.length;
    Logger.log(idLength);
    var container = [];
    for (var i = 0; i < idLength; i  ) {            
        Logger.log(i);
        container.push({
            id: wooProducts[i]["id"],
            sku: wooProducts[i]["sku"],
            price: data[i]["price"],
        });
        
        Logger.log(container);
        var wooBatch = JSON.stringify(container);
        Logger.log(wooBatch);           
      }        
}

}

// 使用“解决”进行最终编辑:

所以我认为一次按 1 个 sku 询问效率低下,所以现在我按 100 个询问,并分页一段时间,然后将 id、sku、price 保存到容器阵列中。我现在需要将容器阵列与具有更新价格的阵列进行比较,并形成一个带有 id、sku 和更新价格的新阵列,我现在正在阅读。代码:

function getDataloopwoo() {
var ck = 'xx'

var cs = 'xx'

var website = 'xx'

var optionsGet =

    {
        "method": "GET",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "muteHttpExceptions": true,

    };    

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PreciosBULK');
var codigos = sheet.getRange("A2:A").getValues();
var precios = sheet.getRange("B2:B").getValues();    

const data = codigos.map(function(codigos, indice) {
    return {
        sku: codigos[0],
        price: precios[indice][0]
    }
})


var container = [];


    var surl = website   "/wp-json/wc/v3/products?consumer_key="   ck   "&consumer_secret="   cs   "&per_page=100";
    var url = surl
    //Logger.log(url)

    var result = UrlFetchApp.fetch(url, optionsGet);
    var headers = result.getAllHeaders();
    var total_pages = headers['x-wp-totalpages'];
    var pages_count = 0;
    while (pages_count < total_pages) {

    if (result.getResponseCode() == 200) {

        var wooProducts = JSON.parse(result.getContentText());
        //Logger.log(result.getContentText());
    }     
    
    
    for (var i = 0; i < wooProducts.length; i  ) {            
        //Logger.log(i);
        container.push({
            id: wooProducts[i]["id"],
            sku: wooProducts[i]["sku"],
            price: wooProducts[i]["price"],
        });
        
        Logger.log(container);            
        
      }
      pages_count  ;
      if (pages_count < total_pages){
        var surl = website   "/wp-json/wc/v3/products?consumer_key="   ck   "&consumer_secret="   cs   "&per_page=100"   "&page="   (pages_count   1);
        var url = surl
        var result = UrlFetchApp.fetch(url, optionsGet);
        Logger.log(url);
                    
      }        
}

}

uj5u.com热心网友回复:

container在回圈的每次迭代中都重置阵列

for (var i = 0; i < idLength; i  ) {
    var container = []; // <-----------------here
    ...
    container.push({
    ...

我认为阵列应该在回圈之外定义:

var container = [];
for (var i = 0; i < idLength; i  ) {
    ...
    container.push({
    ...
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *