【发布时间】:2014-08-07 16:08:56
【问题描述】:
单击字段名称的 ASC 或 DESC 链接并创建键:值对对象并将其推送到数组; 如果键存在,则将值替换为选定的单击事件并更新数组键:值;
例子
单击按名称排序 - DESC:
[{"name":"desc"}]
再次单击按名称排序 - ASC:
[{"name":"asc"}]
点击按年龄排序 - DESC:
[{"name":"asc"},{"age":"desc"}]
再次单击按名称排序 - DESC:
[{"name":"desc"},{"age":"desc"} ]
$scope.clickME = function(fieldName, orderType) {
var obj = {};
obj[fieldName] = orderType;
updateArray($scope.sortList, obj);
}
var updateArray = function(array, newObject) {
//console.log(newObject);
var hash = {};
var i = 0;
for (i = 0; i < array.length; i++) {
hash = array[i];
console.log(array[i]);
console.log(hash.hasOwnProperty(newObject));
//How to check the key is same? not the value as am passing same key but value are different
if (!hash.hasOwnProperty(newObject)) {
//check for the value and replace it
// object[fieldName] = (newObject[fieldName] === 'asc') ? 'desc' : 'asc';
// return;
}
}
array.push(newObject);
};
【问题讨论】:
-
这里有什么问题?
-
在数组中查找自定义字段名(姓名、年龄或性别)并将其值替换为“asc”或“desc”;
-
什么数组?您发布的代码中没有数组。代码应发布在本网站上,而不是从其他地方链接。
-
@Pointy :分享了实际功能所在的演示链接!
标签: javascript jquery underscore.js arrays