0

working with Impactjs, a game engine, here and levels have this very strange setup:

[
    {
       "entities": [
            {"type":"type1","x":100,"y":100,"settings":{"directsTo":"-5"}},
            {"type":"type2","x":101,"y":101,"settings":{"directsTo":"-4"}}
        ],
        "layer": [
            other data
         ]
    }
]

I'm wondering how one gets the index of the type1 object based off of the directsTo property of the settings object?

Javascript or jQuery would be fine.

Edit: The game has to work on smoothly on mobile so having an efficient solution is good.

dlkulp
  • 1,849
  • 4
  • 26
  • 42
  • 2
    A simple forloop can do that. However,for repetitive complex calculations like this,I would suggest you to include underscore.js in your stack. http://underscorejs.org/#where – DhruvPathak Apr 03 '14 at 06:54
  • would a for loop be faster than some jQuery or javascript function? Working with mobile and we already have performance issues. – dlkulp Apr 03 '14 at 06:56
  • 1
    There is no common functions for get index of object. But you can get create your own function for get index. As example, same question: http://stackoverflow.com/questions/19111224/get-index-of-array-of-objects-via-jquery – Виктор Новиков Apr 03 '14 at 06:56
  • @dlkulp I meant a function having a forloop to identify the object and return the index number. – DhruvPathak Apr 03 '14 at 06:59

4 Answers4

1

Try this,

var arr =[{
    "entities": [{
        "type": "type1",
        "x": 100,"y": 100,
        "settings": {"directsTo": "-5"}
    }, {
        "type": "type2",
        "x": 101,"y": 101,
        "settings": {"directsTo": "-4"}
    }],
    "layer": ['other data']
}];
var t='type1';
var newArr=arr[0];
for(var data in newArr){
    for(a in newArr[data]){
        if(newArr[data][a].type == t){
             alert('Index of '+t+' is '+a+' in '+data);
        }
    }
}

Live Demo

Updated demo

Rohan Kumar
  • 38,998
  • 11
  • 69
  • 99
0

Can you use the filter property?

Assuming your JS object looks like this

var j = [
    {
       "entities": [
            {"type":"type1","x":100,"y":100,"settings":{"directsTo":"-5"}},
            {"type":"type2","x":101,"y":101,"settings":{"directsTo":"-4"}}
        ],
        "layer": [
            "otherdata":{}
         ]
    }
];

You can find the object using

var result = j[0].entities.filter(function(n) { return n.settings.directsTo == "-5"; });
// result[0].type == "type1"
jasonscript
  • 5,189
  • 1
  • 23
  • 39
  • I'm not concerned with the rest of the object, I just need the index of the object in the entities array. – dlkulp Apr 03 '14 at 07:04
0

You can create a function which gets the index of an object among other objects, for example like this

//assuming you have the data parsed as a JSON object "data"
//and you also have your entity object as "obj"
function getIndex(obj, data){
    return data.entities.indexOf(obj);
}

if you don't have the "obj" object you will have to create a function which first finds the correct object based on an attribute, for example the type parameter

function findEntity(type, source){
    for(var i=0; i<source.entities.length; i++){
        if(source.entities[i].type == type){
            return source.entities[i];
        }
    }
    return false;
}

now you can call it like this

getIndex(findEntity("type1", data), data);

Hope it helps you start off!

Dropout
  • 12,137
  • 9
  • 47
  • 96
0

Thank you to Rohan Kumar and Виктор Новиков.

var array =[
    {
        "entities": [
            {
                "type": "type1",
                "x": 100,"y": 100,
                "settings": {"directsTo": "-5"}
            },
            {
                "type": "type2",
                "x": 101,"y": 101,
                "settings": {"directsTo": "-4"}
            }
        ],
        "layer": ['other data']
    }
];

function getArrayIndexForLocationKey(arr, val) {
    for(var i = 0; i < arr.length; i++){
        if(arr[i].settings.directsTo == val)
            return i;
    }
    return -1;
}

live here

dlkulp
  • 1,849
  • 4
  • 26
  • 42