28

Let's say I receive a JSON-string containing information about people. Now I loop through all the JSON-objects and extract the name and age or each person.

How would I store these as tuples in a list for further processing? (E.g. by another function)

Boris
  • 8,551
  • 25
  • 67
  • 120

5 Answers5

17

Assuming the incoming JSON is something like the following:

var incomingList = [{
  "name": "Homer Simpson",
  "age": 45,
  "occupation": "Nuclear Plant Technician",
    ...
},{
  "name": "Marge Simpson",
  "age": 44,
  "occupation": "Homemaker",
    ...
},
  ...
]

You could extract the name and age only into a similar array of objects:

var nameAndAgeList = incomingList.map(function(item) {
  return {
    name: item.name,
    age: item.age
  };
});

JavaScript only has objects and arrays, not tuples. If your data is associative (each record has key-value-pairs with the field name as the key) then the above approach will work fine.


However, if you need to associate the names and ages in a single object as key-value-pairs (like an associative array) with names as keys and ages as values, the following will work (assuming the names are unique):

var namesAndAges = incomingList.reduce(function(result,item) {
  result[item.name] = item.age;
  return result;
},{});
Roy Tinker
  • 10,044
  • 4
  • 41
  • 58
6

JS doesn't have a "tuple" type of object. (see: JavaScript Variable Assignments from Tuples)

You can either create your own tuple-class or simply use an array of length 2 (so you end up with an array of length 2 arrays)

Community
  • 1
  • 1
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
6

Building on Vikram's answer, as you have specifically asked for tuples, the following returns you an array of arrays - in other words, a list of tuples.

var parsed = JSON.parse('[{"name":"john", "place":"usa", "phone":"12345"},{"name":"jim", "place":"canada", "phone":"54321"}]');

var people = [];
for (var i=0; i < parsed.length; i++) {
   var person = [parsed[i].name, parsed[i].place, parsed[i].phone];
   people.push(person);
}
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
3

What don't you convert the JSON string itself into a object. JSON is natively supported in javascript after all it is *J*ava*S*cript*O*bject*N*otation.

Suppose you have a json string -

"[{name:john,place:usa,phone:12345},{name:jim,place:canada,phone:54321}]"

You can convert into a js object by -

var persons = JSON.parse("[{name:john,place:usa,phone:12345},{name:jim,place:canada,phone:54321}]");

Now you can access it like this - looping all the tuples/records -

for (var i=0;i<persons.length;i++) {
   var person = persons[i];
   var name = person.name;
   var place = person.place;
   var phone = person.phone;
}
Vikram Rao
  • 514
  • 3
  • 16
1

If by tuple you mean something like an ordered pair, you can have an object with the attribute name as the 'name' of the person and the value as the 'age' of the person.

Like this:

var tupleObj = {};
for (var i in jsonObj) {
    tupleObj[jsonObj[i].name] = jsonObj[i].age;
}
tewathia
  • 6,890
  • 3
  • 22
  • 27