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.

4
  • 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. underscorejs.org/#where Commented Apr 3, 2014 at 6:54
  • would a for loop be faster than some jQuery or javascript function? Working with mobile and we already have performance issues. Commented Apr 3, 2014 at 6: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: stackoverflow.com/questions/19111224/… Commented Apr 3, 2014 at 6:56
  • @dlkulp I meant a function having a forloop to identify the object and return the index number. Commented Apr 3, 2014 at 6:59

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

close, doesn't get the id of the containing object with the correct property from the settings object
Here directsTo is your id, -5 in the above case? See my updated demo once.
again, closer, the problem is that I don't care about type at all, I just need the index of the object with the directsTo value that I'm searching with.
yup, that's what I needed!
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"

1 Comment

I'm not concerned with the rest of the object, I just need the index of the object in the entities array.
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!

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.