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

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

本文关键字:函数 运行 对象 何遍历 遍历 javascript      更新时间:2023-09-26

我是JS的新手。

我在NetSuite中设置了一个保存的搜索,为我们提供了项目的图像字段(包含URL)。我现在正在 NS 中设置一个脚本来测试这些字段以查看哪些项目字段返回 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);
    }
}

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

即使您在搜索结果中得到结果。你正在做的循环是错误的。你将得到的数组类似于[{},{},{}],即对象数组。

要访问,您需要

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