从对象数组中获取键值数组,而不知道对象数组的格式(Javascript)?

时间:2014-05-04 19:34:40

标签: javascript jquery arrays underscore.js

想象一下,我给出了一系列相似对象的引用,例如: array将是该数组的名称。现在我要求创建一个数组,其中包含在该数组的每个对象中找到的某些属性的所有值,例如"user.id"

问题在于我不知道每个对象的格式以及该属性将驻留/嵌套的位置。所以"user.id"可能位于array[#].someKeyarray[#].someKey["user.id"])或在array[#].someKey.someOtherKeyarray[#].someKey.someOtherKey["user.id"]

是否有可以创建这样一个数组的函数(jQuery,underscore..etc)? 例如var arrayOfUserIds = returnArray(array, "user.id");

例如,假设以下是此类数组的示例:

var array = [
{
  "age": "13",
  "type": "publish_action",
  "tag": null,
  "timestamp": 1398931707000,
  "content": {
    "action": "publish",
    "user.id": "860",
    "user.email": "alex@somemail.com",
    "property.id": "2149",
    "iteration_id": "15427",
    "test_id": "6063",
    "property.name" : "bloop"
}, {
  ....
}, {
  ....
}];

基于以上所述,我显然可以做到:

var arrayOfUserIds = [];

for (var i=0; i<array.length; i++)
{
  arrayOfUserIds.push(array[i]["content"]["user.id"]);
}

但就像我说的那样,在我的情况下,我不知道对象的格式,所以我不能创建这样的for循环。

任何想法都将不胜感激!

谢谢!

2 个答案:

答案 0 :(得分:2)

如果我理解正确,someArray中的每个对象都包含一个属性user.id或一个包含user.id的对象......或者递归地包含一个包含someArray的对象}。您想要创建仅包含user.id属性的数组。

执行此操作的简单方法是对数组中的每个对象进行递归检查,直到user.id找到:

// get `user.id` property from an object, or sub-object
// it is assumed that there will only be one such property;
// if there are more than one, only the first one will be returned
function getUserId(o){
    if(o===null || o===undefined) return;
    if(o['user.id']) return o['user.id'];
    for(var p in o){
        if(!o.hasOwnProperty(p)) continue;
        if(typeof o[p] !== 'object') continue;
        if(o[p] === null || o[p] === undefined) continue;
        var id = getUserId(o[p]);
        if(id) return id;
    }
}

function getUserIds(arr){
    return arr.map(function(e){
        return getUserId(e);
    });
}

如果你想要一些更通用的东西,你可以写一个&#34; find&#34;将在对象树中查找命名属性的所有实例的方法:

 var find = (function(){
    function find(matches, o, prop, checkPrototypeChain){
        if(typeof o[prop] !== 'undefined') matches.push(o[prop]);
        for(var p in o){
            if(checkPrototypeChain || !o.hasOwnProperty(p)) continue;
            if(typeof o[p] !== 'object') continue;
            if(o[p] === null || o[p] === undefined) continue;
            find(matches, o[p], prop, checkPrototypeChain);
        }
    }
    return function(o, prop, checkPrototypeChain){
        var matches = [];
        find(matches, o, prop, checkPrototypeChain);
        return matches;
    }
})();

然后你可以根据它来映射你的数组:

var userIds = someArray.map(function(e){ return find(e, 'user.id'); });

请注意,我对可能在原型链中的属性进行了上釉,但在find函数中,我添加了另外搜索原型链中的属性的功能。

答案 1 :(得分:1)

我假设你只使用原语和对象/数组文字。在这种情况下,以下方法(使用下划线)似乎可以解决问题。

var testSubject = {
    mykey: 9,
    firstArray: [
        {something: 9, another: {x: 'hello', mykey: 'dude'}, mykey: 'whatever'},
        {something: 9, another: {x: 'hello', mykey: 'dude2'}, mykey: 'whatever2'},
        {
            someArray: [
                {seven: 7, mykey: 'another'},
                {hasNo: 'mykey', atAll: 'mykey'}
            ]
        }
    ],
    anObject: {beef: 'jerky', mykey: 19}
};

function getValuesForKey(subject, searchKey) {
    return _.reduce(subject, function(memo, value, key) {
        if (_.isObject(value)) {
            memo = memo.concat(getValuesForKey(value, searchKey));
        } else if (key === searchKey) {
            memo.push(value);
        }
        return memo;
    }, []);
}

console.log(getValuesForKey(testSubject, 'mykey'));
// -> [9, "dude", "whatever", "dude2", "whatever2", "another", 19] 

它只返回值列表,因为它们将共享相同的键(即指定的键)。另外,我相信如果它们的值不是原始值,则会忽略任何匹配的键(例如mykey: {…}mykey: […]应该被忽略)。我希望它有所帮助。

相关问题