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

javascript - Find index in array of objects - Stack Overflow

programmeradmin8浏览0评论

I would like to find index in array. Positions in array are objects, and I want to filter on their properties. I know which keys I want to filter and their values. Problem is to get index of array which meets the criteria.

For now I made code to filter data and gives me back object data, but not index of array.

var data =  [
        {
            "text":"one","siteid":"1","chid":"default","userid":"8","time":1374156747
        },
        {
            "text":"two","siteid":"1","chid":"default","userid":"7","time":1374156735
        }
    ];

var filterparams = {userid:'7', chid: 'default'};

function getIndexOfArray(thelist, props){
    var pnames = _.keys(props)
    return _.find(thelist, function(obj){
        return _.all(pnames, function(pname){return obj[pname] == props[pname]})
    })};

var check = getIndexOfArray(data, filterparams ); // Want to get '2', not key => val

I would like to find index in array. Positions in array are objects, and I want to filter on their properties. I know which keys I want to filter and their values. Problem is to get index of array which meets the criteria.

For now I made code to filter data and gives me back object data, but not index of array.

var data =  [
        {
            "text":"one","siteid":"1","chid":"default","userid":"8","time":1374156747
        },
        {
            "text":"two","siteid":"1","chid":"default","userid":"7","time":1374156735
        }
    ];

var filterparams = {userid:'7', chid: 'default'};

function getIndexOfArray(thelist, props){
    var pnames = _.keys(props)
    return _.find(thelist, function(obj){
        return _.all(pnames, function(pname){return obj[pname] == props[pname]})
    })};

var check = getIndexOfArray(data, filterparams ); // Want to get '2', not key => val
Share Improve this question asked Jul 18, 2013 at 16:09 norbertnorbert 5252 gold badges6 silver badges16 bronze badges 1
  • 2 not really an answer but you could have used _.findWhere to get exactly the same result as what you have now – soulcheck Commented Jul 18, 2013 at 16:18
Add a ment  | 

4 Answers 4

Reset to default 11

Using Lo-Dash in place of underscore you can do it pretty easily with _.findIndex().

var index = _.findIndex(array, { userid: '7', chid: 'default' })

here is thefiddle hope it helps you

 for(var intIndex=0;intIndex < data.length; intIndex++){
  eachobj = data[intIndex];
var flag = true;
 for (var k in filterparams) {

    if (eachobj.hasOwnProperty(k)) {
        if(eachobj[k].toString() != filterparams[k].toString()){
           flag = false;
        }
    }
}
if(flag){
       alert(intIndex);
}

}

I'm not sure, but I think that this is what you need:

var data =  [{
    "text":"one","siteid":"1","chid":"default","userid":"8","time":1374156747
}, {
    "text":"two","siteid":"1","chid":"default","userid":"7","time":1374156735
}];
var filterparams = {userid:'7', chid: 'default'};

var index = data.indexOf( _.findWhere( data, filterparams ) );

I don't think you need underscore for that just regular ole js - hope this is what you are looking for

var data =  [
        {
            "text":"one","siteid":"1","chid":"default","userid":"8","time":1374156747
        },
        {
            "text":"two","siteid":"1","chid":"default","userid":"7","time":1374156735
        }
    ];

var userid = "userid"
var filterparams = {userid:'7', chid: 'default'};
var index;
for (i=0; i < data.length; i++) {
    for (prop in data[i]) {
        if ((prop === userid) && (data[i]['userid'] === filterparams.userid)) {
            index = i
        }
    }
}

alert(index);
发布评论

评论列表(0)

  1. 暂无评论