0

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. 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 :

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

case - 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.

reciever
  • 69
  • 2
  • 11
  • Use the combination of index and key to access it. Ex- `arr[0][0]['idProperty'].args[1].salary` – Hassan Imam Feb 12 '19 at 12:43
  • `arr[0][x]['idProperty']['args'][1]['salary'];` - replace x with item id. Ideally you'd want to clean up your object. There are too many layers. – aullah Feb 12 '19 at 12:46

5 Answers5

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);
random
  • 7,756
  • 3
  • 19
  • 25
0

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

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

0

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);
}
Durgesh
  • 205
  • 2
  • 9
0

map over the nested array and find the salary object in the args array of each object. 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.

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);
Andy
  • 61,948
  • 13
  • 68
  • 95
  • it would be a great help if you can consider the cases I added. – reciever Feb 12 '19 at 13:25
  • can you please explain what is happening in your find statement find(obj => Object.keys(obj)[0] === 'salary').salary and why is "salary" is used two times – reciever Feb 12 '19 at 14:15
  • `find` returns the object in the array the (only/first) key of which is "salary". It then extracts the value of the salary property from that object. @reciever – Andy Feb 12 '19 at 17:36
  • Also @reciever case 1 isn't valid JS, and it looks like the only change in case 2 is that there isn't a nested array so you would do `arr.map` rather than `arr[0].map`. – Andy Feb 12 '19 at 17:39
0

After trying randomly for a long time I got answer for the case 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.

reciever
  • 69
  • 2
  • 11
  • "https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json" this might be helpful for beginners. – reciever Feb 15 '19 at 05:59