如果您正在使用javascript查找对象数组的Get / Delete / Sum / IsExist函数,我已发布此问题并回答此类函数
答案 0 :(得分:0)
顺便说一句,你的问题与JSON没什么关系,而且与jQuery无关。
答案 1 :(得分:0)
只需使用underscore。
从简单数组中删除:
var newList = _.without(list, 'hello', 'world'); // Remove any instance of 'hello' and 'world'
从对象数组中删除:
var toRemove = _.where(list, {title: 'hello', subtitle: 'world'});
var newList = _.difference(list, toRemove);
阵列中存在:
var exists = _.contains(list, value);
从数组中获取项目:
var item = _.find(list, function(i){ i === value }); // For simple types
var item = _.findWhere(list, {title: 'hello', subtitle: 'world'}); // For complex types
总结项目:
var sum = _.reduce(list, function(start, num){ return start + num; }, 0);
还有很多。
答案 2 :(得分:-2)
4个Javascript助手
var JShelpers = { removeItemFromArray: function (myObjects, prop, valu) { return myObjects.filter(function (item) { return item[prop] !== valu; }); }, isExistInArray: function (myObjects, prop, valu) { var i = myObjects.length; while (i--) { if (myObjects[i][prop] == valu) { return true; } } return false; }, getItemFromArray: function (myObjects, prop, valu) { var i = myObjects.length; while (i--) { if (myObjects[i][prop] == valu) { return myObjects[i]; } } return ""; }, sumItemInArray: function (myObjects, prop) { var summation = 0; myObjects.forEach(function (item) { summation += parseFloat(item[prop]); }); return summation; } }
示例:强> 假设您有一个像这样的Json格式的Employees数组
var employeesArray = [{"Id":1,"Name":"tom","Age":15},{"Id":2,"Name":"Harry","Age":17}];
并且您希望通过他的年龄检索员工不仅ID(通用), 你可以直接打电话
JShelpers.getItemFromArray(employeesArray,"Age",15)