duanmibei1929 2014-12-28 19:52
浏览 97

AngularJS过滤器orderBy - 数字似乎被视为字符串

I have an array of objects which is obtained from JSON in PHP and all properties come back as a string (in quotes) the property required comes back as an int from the API.

I have a particular property let's say 'value' which I convert to integer type (using parseInt()) for each object in the array. I want to apply the orderBy $filter on this property but the order comes back in lexicographical order ( i.e. 9 then 88 then 8... etc) which as far as I'm aware means 'value' is being considered as a string.

The way I apply the filter is as follows:

scope.sortedItems = $filter('orderBy')(scope.sortedItems, sortItems, true);

which uses the function sortItems:

var sortItems = function(item) {
    return [item[scope.orderProp], mapping[item.item_rarity]];
};

to apply secondary ordering as well as the primary.

I'm confused as I've also run isNaN on each property everytime the filter is applied and for each object, it comes back as false (is a number). There are some null values but I would assume this doesn't affect anything?

edit: modified the returned JSON to return integers but still no luck.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入