简体   繁体   中英

get index of object in array in object in array

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.

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

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"

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM