Netsuite Javascript抓取最后一个数组值

Netsuite Javascript Grab Last Array Value

本文关键字:数组 最后一个 抓取 Javascript Netsuite      更新时间:2023-09-26

所以我在这个网站上找到了一些关于如何去抓取数组的最后一个索引值的信息。我有一个长度未知的数组。它建立在搜索结果的基础上。例如:

var custid = nlapiGetFieldValue('entity');
    var custRecord = nlapiLoadRecord('customer', custid);
    var itemPriceLineCount = custRecord.getLineItemCount('itempricing');
    for (var i = 1; i <= itemPriceLineCount; i++) {
        var priceItemId = [];
        priceItemId = custRecord.getLineItemValue('itempricing', 'item', i);
        if (priceItemId == itemId) {
            var histCol = [];
            histCol[0] = new nlobjSearchColumn('entity');
            histCol[1] = new nlobjSearchColumn('totalcostestimate');
            histCol[2] = new nlobjSearchColumn('tranid');
            histCol[3] = new nlobjSearchColumn('trandate');
            var histFilter = [];
            histFilter[0] = new nlobjSearchFilter('entity', null, 'is', custid);
            histFilter[1] = new nlobjSearchFilter('item', null, 'is', itemId);
            var histSearch = nlapiSearchRecord('invoice', null, histFilter, histCol);
            for (var h = 0; h <= histSearch.length; h++) {
                var itemRate = new Array();
                var histSearchResult = histSearch[h];
                itemRate = histSearchResult.getValue('totalcostestimate');


            }
        }
    }

现在当我使用:

var last_element = itemRate[itemRate。]长度- 1];

它给出了数组中每个元素中的数字/占位符的数量。因此,根据我的例子,我知道我的数组保存值为。00和31.24,因为我把它们放在那里进行测试。所以last_element的结果是3和5。如何获取值31.24或最后一个元素周期?我需要的是数值而不是位数

var itemRate = new Array();// Not sure what you intend to do with this array
var histSearchResult = histSearch[h];
itemRate = histSearchResult.getValue('totalcostestimate'); // but note `itemRate` is no more an array here. Its a variable having the value of `totalcostestimate` in string format

现在来看你的用例

    /* you're trying to get the length of the string value and subtracting -1 from it.
       So its very obvious to get those number of digits */
        var last_element = itemRate[itemRate.length - 1]; // returns you that index value of the string

如果你想获得你的搜索的最后一个数组值,即histSearch

你可能想这样做

var last_element = histSearch[histSearch.length-1].getValue('totalcostestimate');

作为旁注,总是建议验证从保存的搜索结果返回的值。因为如果搜索成功,它会返回一个数组对象,另一方面,如果没有找到结果,它会返回null

//likely to get an error saying can't find length from null
    for (var h = 0; h <= histSearch.length; h++) {
    }

你可以这样写

// Never enter into the loop if it is null
        for (var h = 0; histSearch!=null && h <= histSearch.length; h++) {
        }