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 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100