gpt4 book ai didi

javascript - 如何迭代 javascript 对象并对每个值运行函数

转载 作者:行者123 更新时间:2023-12-03 07:24:54 26 4
gpt4 key购买 nike

我是 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);
}
}

最佳答案

在这里我可以看到 searchResults 变量在循环时将为空。由于您对 searchItems 函数的调用是异步的。这将需要一些时间来执行,因为我猜它会从 API 获取数据。当它返回值时,您的循环也将被执行。您可以通过放置警报(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

关于javascript - 如何迭代 javascript 对象并对每个值运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36042664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com