Netsuite Javascript 获取最后一个数组值

Netsuite Javascript Grab Last Array Value

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

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.length - 1];

它给出了数组每个元素中 digits/placeholders 的数量。因此,根据我的示例,我知道我的数组包含 .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');

作为旁注,始终建议从保存的搜索结果中验证 returning 值。因为如果搜索成功,它会 return 给你一个数组对象,另一方面,如果没有找到结果,它会 return 你 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++) {
        }