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

时间:2019-02-12 12:37:55

标签: javascript arrays react-native

我从响应中得到一个数组,我需要从每个对象/响应中获取id Proprety属性的args数组的sals属性中存在的值。如何实现呢?

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

编辑:感谢上述查询的所有答案,但是如果结构为,如何获得相同的薪水属性:

案例1:

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

案例-2

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

很抱歉问这是否如此简单,因为我真的是本机和Java脚本的新手,所以对各种情况感到困惑。

5 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

您可以将for用作

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

您将看到以下内容:

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

更新

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

答案 2 :(得分:0)

嗨,您可以尝试以下方法-

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);
}

答案 3 :(得分:0)

map在嵌套数组上,find在每个对象的args数组中的salal对象。这将返回一组薪水数字。我使用了find而不是访问args数组的第二个元素,以防薪水对象不总是第二个元素和某些object destructuring

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);

答案 4 :(得分:0)

长时间尝试后,我得到了案例2的答案

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

但由于Andy使用lodash而无法使用arg [1],因此我无法在obj.idProperty.args[1].salary时获得答案。