最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery filter object by value with inconsistent index - Stack Overflow

programmeradmin1浏览0评论

I have an object similar to

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

I am trying to filter based on value using this

$.filter(obj,function(i, value){
  return value>3;
});

However this is returning empty.

Expected output {'Sand': 4 }

Is there a way to filter by value, when the indexes of the objects cannot be consistently addressed and may vary.

I have an object similar to

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

I am trying to filter based on value using this

$.filter(obj,function(i, value){
  return value>3;
});

However this is returning empty.

Expected output {'Sand': 4 }

Is there a way to filter by value, when the indexes of the objects cannot be consistently addressed and may vary.

Share Improve this question edited Apr 11, 2016 at 14:11 Joel asked Apr 11, 2016 at 14:00 JoelJoel 1,6902 gold badges22 silver badges34 bronze badges 3
  • 2 What is your desired output? – Zakaria Acharki Commented Apr 11, 2016 at 14:07
  • @ZakariaAcharki I have updated it with expected output – Joel Commented Apr 11, 2016 at 14:11
  • 1 You shouldn't use jQuery to do that. It's a easy operation do with native JavaScript and it will be faster too. Take a look on the answers bellow. – Ricardo França Commented Apr 11, 2016 at 14:20
Add a comment  | 

6 Answers 6

Reset to default 6

Try something like this..

function filterObjectProperties(obj, filtercb){
    var ret = {};
    for(var p in obj)
        if(obj.hasOwnProperty(p))
            if(filtercb(obj[p]))
                ret[p] = obj[p];
    return ret;
}

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

var newObj = filterObjectProperties(obj, function(val){
    return val > 3;
});

https://jsfiddle.net/dht2L55L/

This can be done without $.filter:

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };
result = {};
for (var k in obj) {
    if (obj[k] > 3) {
        result[k] = obj[k];
    }
}
console.log(result);

You could you JQuery.each(). To use JQuery.filter and JQuery.grep, I think your object should be formed different.

$(function(){

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

var result = null;

$.each(obj, function(key, value) {
   if(value > 3){
   result = key;
   }
}); 

console.log(result);

});

There is no native filter to the Object object, but how about this:

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };
newObject = {}
Object.keys(obj).map(function(value, index) {
    if (obj[value]>3){
       newObject[value] = obj [value]
    }
});
snippet.log(newObject);
// => {'Sand': 4 }
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Should be as simple as

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

var result = Object.keys(obj)
    .filter(function(e) { return obj[e] > 3 })
    .reduce(function(object, property) { 
         return (object[property] = obj[property], object);
     }, {})

without any library

var obj = { 'Earth': 1, 'Sky': 2, 'Tree': 3, 'Sand': 4 };

function filterObj(obj, valueThreshold) {
  var keys = Object.keys(obj);
  var result = {};
  keys.forEach(function(key) {
    var value = obj[key];
    if (value > valueThreshold) {
      result[key] = value;
    }
  })
  return result;
}

console.log('obj: ' + JSON.stringify(obj));
var filteredObj = filterObj(obj, 3);
console.log('filterdObj: ' + JSON.stringify(filteredObj));
发布评论

评论列表(0)

  1. 暂无评论