简体   繁体   English

获取数组中对象的索引

[英]get index of object in array in object in array

working with Impactjs, a game engine, here and levels have this very strange setup: 与游戏引擎Impactjs一起使用时,此处和关卡的设置非常奇怪:

[
    {
       "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? 我想知道如何根据设置对象的directsTo属性获取type1对象的索引?

Javascript or jQuery would be fine. Javascript或jQuery都可以。

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? 您可以使用filter属性吗?

Assuming your JS object looks like this 假设您的JS对象看起来像这样

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 如果没有“ obj”对象,则必须创建一个函数,该函数首先根据属性(例如类型参数)找到正确的对象

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 Виктор Новиков. 感谢Rohan Kumar和ВикторНовиков。

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 住在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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