如何遍历 javascript 对象和 运行 每个值的函数

How to iterate through javascript object and run a function on each value

我是 JS 新手。

我在 NetSuite 中设置了一个保存的搜索,它为我们提供了我们项目的图像字段(包含 URL)。我现在正在 NS 中设置一个脚本来测试这些字段以查看哪些项目字段 return 404(即需要修复)。

我的问题是,如何设置函数 imageURLValidator 来遍历函数 searchItems 的字段值?

下面是我开始的过程,但显然有很多不正确的语法。

function imageURLValidator() {  
    var searchResults = searchItems('inventoryitem','customsearch529');

    var url = '';
    var item = '';
    var field = '';

    //insert loop here to iterate over items in searchResults array

    //loop through items
    for (var i = 0, i > searchResults[inventoryObject].length, i++) {
        item = searchResults.[inventoryObject].[i];

        //loop through fields in item
        for (var f = 2, f > item.length, f++) {
            field = item[f];

            //check URL via item field's value 
            var code = checkURL(item[field].getvalue([field]));

            //generate error based on code variable 
            createErrorRecord(code,item,field)
        }
    }
}

function searchItems(type, searchid) {
    //defining some useful variables that we will use later
    var inventoryArray = [];
    var count = 0;

    //loading the saved search, replace the id with the id of the search you would like to use
    var inventoryItemSearch = nlapiLoadSearch(type, searchid);

    //run the search
    var inventoryItemResults = inventoryItemSearch.runSearch();

    //returns a js array of the various columns specified in the saved search
    var columns = inventoryItemResults.getColumns();

    //use a do...while loop to iterate through all of the search results and read what we need into one single js object

    do {
        //remember the first time through the loop count starts at 0
        var results = inventoryItemResults.getResults(count, count + 1000.0);

        //we will now increment the count variable by the number of results, it is now no longer 0 but (assuming there are more than 1000 total results) will be 1000 
        count = count + results.length;

        //now for each item row that we are on we will loop through the columns and copy them to the inventoryObject js object
        for (var i=0; i<results.length; i++){
            var inventoryObject = {};
            for (var j=0; j<columns.length; j++){
                inventoryObject[columns[j].getLabel()] = results[i].getValue(columns[j]);
            }

            //then we add the inventoryObject to the overall list of inventory items, called inventoryArray
            inventoryArray.push(inventoryObject);
        }

    //we do all of this so long as the while condition is true.  Here we are assuming that if the [number of results]/1000 has no remainder then there are no more results        
    } while (results.length != 0 && count != 0 && count % 1000 == 0);

    return inventoryArray;
}

function checkURL(url) {
    var response = nlapiRequestURL(url);
    var code = response.getCode();

    return code;
}

function createErrorRecord(code,item,field) {
    if (code == 404){
        //create error record
        var errorRecord = nlapiCreateRecord('customrecord_item_url_error');
        errorRecord.setFieldValue('custrecord_url_error_item', item);
        errorRecord.setFieldValue('custrecord_url_error_image_field', field);
    }
}

在这里我可以看到 searchResults 变量在循环时将为空。由于您对 searchItems 函数的调用是异步的。这将需要一些时间来执行,因为我猜它会从 API 获取数据。当它 returns 值时,你的循环也将被执行。您可以通过放置警报 (searchResults.length) 或 console.log(searchResults.length) 来对此进行测试。为此,您需要使用 回调函数

即使您在 searchResults 中获得结果也是如此。你做的循环是错误的。您将获得的数组类似于 [{},{},{}],即对象数组。

要访问您需要

for (var i = 0, i > searchResults.length, i++) {
        var inventoryObject = searchResults[i] // your inventoryObject 

    for(var key in inventoryObject){
        item = inventoryObject[key]; // here you will get each item from inventoryObject 
        //loop through fields in item
        for (var f = 2, f > item.length, f++) {
            field = item[f];

            //check URL via item field's value 
            var code = checkURL(item[field].getvalue([field]));

            //generate error based on code variable 
            createErrorRecord(code,item,field)
        }
    }
}

是的,欢迎来到 Javascript