Netsuite Javascript抓住最后一个数组值

时间:2015-05-19 21:43:13

标签: javascript arrays netsuite

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

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];

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

1 个答案:

答案 0 :(得分:0)

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++) {
        }