3

I have some data which looks like this:

{
  "obj":
  [
    {
      "name": "name1",
      "age": "24"
    },
    {
      "name": "name2",
      "age": "17"
    }
  ]
}

What I need to do is to create 2 arrays from it.

For example:

namelist[];
agelist[];

so the result would be:

namelist: ['name1', 'name2'];

agelist: [24, 17];

My question is, how can I do this?

Nhan
  • 3,595
  • 6
  • 30
  • 38

5 Answers5

2
var namelist = [];
var agelist = [];
for(var i in obj.obj){
    namelist.push(obj.obj[i].name);
    agelist.push(obj.obj[i].age);
}

console.log(namelist, agelist);
KoIIIeY
  • 533
  • 1
  • 7
  • 26
2

Is this what U wanted ?

var zz={
    "obj": [
        {
            "name": "name1",
            "age": "24"
        },
        {
            "name": "name2",
            "age": "17"
        }
    ]
}

namelist=[];
agelist=[];
zz.obj.forEach(function(rec){
  namelist.push(rec.name);
  agelist.push(rec.age);
})

console.log(namelist,agelist)
O_Z
  • 1,515
  • 9
  • 11
0

Make use of jquery map function or otherwise you can loop over the object and push it into array using javascript for loop and use the push() function. Refer Loop through an array in JavaScript

Jquery

var data = {
    "obj": [
        {
            "name": "name1",
            "age": "24"
        },
        {
            "name": "name2",
            "age": "17"
        }
    ]
}

var name = $.map(data.obj, function(value, index) {
    return value.name;
});
var age = $.map(data.obj, function(value, index) {
    return value.age;
});

console.log(name);
console.log(age);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Javascript

var data = {
        "obj": [
            {
                "name": "name1",
                "age": "24"
            },
            {
                "name": "name2",
                "age": "17"
            }
        ]
    }
    var names = [], ages=[];
    data.obj.forEach(function(value, index) {

        names.push(value.name);
        ages.push(value.age);
    });
console.log(names,ages);
Community
  • 1
  • 1
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • 1
    Where did you see a jQuery reference? – Andreas Nov 01 '16 at 11:17
  • 1
    @Andreas I understand the is no jquery tag, i.e why I also added to my answer on how to approach a javascript solution. But since you emphasised on it, I edited my answer. And its OP choice to use jquery if he wishes to, I just gave an option. – Shubham Khatri Nov 01 '16 at 11:31
0
var arr = $.map(myObj, function(value, index) {
return [value];
});


console.log(arr);

if you are not using Jquery then:

var arr = Object.keys(myObj).map(function (key) 
 { return obj[key]; 
});`
felix91
  • 452
  • 3
  • 11
  • 1. There is no jQuery tag; 2. What is `myObj`? 3. The first would create an array of arrays – Andreas Nov 01 '16 at 11:16
  • myObj is the name of the object he wishes to convert – felix91 Nov 01 '16 at 11:18
  • Then your solution makes no sense at all. Either he has to call the snippet for each entry in `"obj"` which would only return an array of values or if he calls it on the object which `"obj"` is a property of, it would result in an array which contains the value of `"obj"` (an array of objects) – Andreas Nov 01 '16 at 11:25
0

You could use this ES6 code, and use the unitary plus for getting the ages as numbers. Assuming your object is stored in variable data:

var namelist = data.obj.map( o => o.name );
var agelist = data.obj.map( o => +o.age );

var data = {
    "obj": [
        {
            "name": "name1",
            "age": "24"
        },
        {
            "name": "name2",
            "age": "17"
        }
    ]
};

var namelist = data.obj.map( o => o.name );
var agelist = data.obj.map( o => +o.age );

console.log(namelist);
console.log(agelist);
trincot
  • 317,000
  • 35
  • 244
  • 286