简体   繁体   English

如何获取存在于数组属性中的值?

[英]How to get value that is present in attribute of an array?

I have an array from a response in which I need to get the value that is present in the salary attribute of args array of id Proprety attribute from each object/response. 我有一个响应数组,我需要从每个对象/响应中获取id Proprety属性的args数组的sals属性中存在的值。 How can achieve this? 如何做到这一点?

 var arr = [
    [
        {
            id: "alex",
            idProperty: {
                args: [
                    {
                    name: "alex_name"
                    }, 
                    { 
                    salary: "16L" 
                    }
                ]
            }
        },
        {
            id: "john",
            idProperty: {
                args: [
                    {
                    name: "john_name"
                    }, 
                    { 
                    salary: "14L" 
                    }
                ]
            }
        }
   ]   
 ];

EDIT: thanks to every one for all the answers for the above query but how to get the same salary attribute if the structure is : 编辑:感谢每个人为上述查询的所有答案,但如果结构为:如何获得相同的薪水属性:

case 1 : 情况1 :

var arr = {
            [
                id: "alex",
                idProperty: {
                    args: [
                        {
                        name: "alex_name"
                        }, 
                        { 
                        salary: "16L" 
                        }
                    ]
                }
            ],
            [
                id: "john",
                idProperty: {
                    args: [
                        {
                        name: "john_name"
                        }, 
                        { 
                        salary: "14L" 
                        }
                    ]
                }
            ]
     };

case - 2 案例-2

var arr = [
                {
                    id: "alex",
                    idProperty: {
                        args: [
                            {
                            name: "alex_name"
                            }, 
                            { 
                            salary: "16L" 
                            }
                        ]
                    }
                },
                {
                    id: "john",
                    idProperty: {
                        args: [
                            {
                            name: "john_name"
                            }, 
                            { 
                            salary: "14L" 
                            }
                        ]
                    }
                }
         ];

I am sorry to ask if this is so simple as I am really new to react native and java script and so confused with various scenarios. 我很遗憾地问这是否如此简单,因为我真的是本机和Java脚本的新手,并且对各种情况感到困惑。

Extract the 'idProperty' Object and then iterate over the args array in it

 var arr = [ [ { id: "alex", idProperty: { args: [ { name: "alex_name" }, { salary: "16L" } ] } }, { id: "john", idProperty: { args: [ { name: "john_name" }, { salary: "14L" } ] } } ] ]; const accu = []; const output = arr[0].forEach(({idProperty}) => { idProperty.args.forEach(({salary}) => { if(salary)accu.push(salary); }); }); console.log(accu); 

You can use a for 您可以使用

var salaries
for(var i = 0; i <arr.length; i++){
   salaries[arr[i].id] = arr.idProperty.args.salary;
}

You'll have something like this: 您将拥有以下内容:

console.log(salaries);
[
'alex':'16L',
'john':'14L'
]

UPDATE UPDATE

_.each(arr, function (value, key) {
      salaries[value.id] = _.find(value.idProperty.args, function(o) { return o.salary != undefined; }).salary;  
});

Hi You can try below approach - 嗨,您可以尝试以下方法-

var newarr = arr[0];
for(var i=0; i<newarr.length; i++)
{
    var salValue = newarr[i].idProperty.args[1].salary
    console.log('Salary value :' + salValue);
}

map over the nested array and find the salary object in the args array of each object. map嵌套数组,然后在每个对象的args数组中find salary对象。 This returns an array of salary figures. 这将返回一组薪水数字。 I've used find instead of accessing the second element of the args array in case the salary object isn't always the second element, and some object destructuring . 我使用find而不是访问args数组的第二个元素,以防薪水对象不总是第二个元素,并且某些对象会被破坏

 var arr = [[{"id":"alex","idProperty":{"args":[{"name":"alex_name"},{"salary":"16L"}]}},{"id":"john","idProperty":{"args":[{"name":"john_name"},{"salary":"14L"}]}}]]; // `map` over the inner array, destructure the args array from each object const salaries = arr[0].map(({ idProperty: { args } }) => // find the object in the args array that has the salary key // and return its salary value args.find(obj => Object.keys(obj)[0] === 'salary').salary ); console.log(salaries); 

After trying randomly for a long time I got answer for the case 2 长时间尝试后,我得到了案例2的答案

const ids = array.map( a => return a.map(obj => obj.idProperty.args[1].salary));

but I am unable to achieve the answer at this point obj.idProperty.args[1].salary without using arg[1] as Andy got using lodash. 但是我现在无法在不使用arg [1]的情况下获得obj.idProperty.args[1].salary的答案,因为Andy使用lodash了。

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

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