Javascript: Manipulate Two Arrays
Solution 1:
You need to use array of objects - eg.
var People = [{ name : 'John', age : 16 }, .... ]
So you can keep age and name together. Then use sort function with custom sorting function
varPeopleSorted = People.sort( function( a, b ) { return a.age - b.age; });
PS. More on Array.sort here - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
Solution 2:
You wouldn't normally have data structured that way as you can see it's excessively complicated to do anything with it.
var names = ['John', 'Ed', 'Sam', 'Alex', 'Mark'];
var ages = [16, 25, 18, 30, 27];
If in some hypothetical situation you actually had to work with that, you could normalize it this way:
var people = names.map(function (name, i) {
return {
name: name,
age: this[i]
};
}, ages);
The result of the mapping is same as writing:
var people = [
{"name":"John","age":16},
{"name":"Ed","age":25},
{"name":"Sam","age":18},
{"name":"Alex","age":30},
{"name":"Mark","age":27}
];
(Yes, the property names are quoted because I cheesed it with JSON.stringify
cos I'm lazy )
And sorting becomes really trivial:
people.sort( function( a, b ) { return a.age - b.age; });
var youngestName = people[0].name,
oldestName = people[people.length-1].name;
So if you have control, use proper structure in the first place.
Solution 3:
var names = ['John', 'Ed', 'Sam', 'Alex', 'Mark'],
ages = [16, 25, 18, 30, 27];
// reduce the ages array to an object containing youngest and oldestvar data = ages.reduce(function (memo, age, index) {
// for every member in the age array// if the age is less then the youngestif (age < memo.youngest.age) {
// then set that to be the youngest age
memo.youngest.age = age;
// and store the name by grabbing it from the names array by index
memo.youngest.name = names[index];
}
// do the same for oldestif (age > memo.oldest.age) {
memo.oldest.age = age;
memo.oldest.name = names[index];
}
return memo;
// create a default seed for this object, // the default youngest age is infinite, the default oldest age is minus infinite
}, {
youngest: {
age: Infinity
},
oldest: {
age: -Infinity
}
});
// data object returned has the same format as the initial seed objectconsole.log(data.youngest.name);
console.log(data.oldest.name);
Solution 4:
var Name = ['John', 'Ed', 'Sam', 'Alex', 'Mark'];
var Age = [16, 25, 18, 30, 27];
var AgeFromName = {};
for (var i = 0; i < Age.length; i++) {
AgeFromName[Age[i]] = Name[i];
}
Age.sort();
alert(AgeFromName[Age[0]]);
Solution 5:
You can create an array of objects, where each object has a name
and age
property, which would allow you to sort the array by age and thus easily get the youngest or oldest. This is covered by at least one other answer, so I won't repeat it here.
Since you mention you are just starting in JavaScript you might like some general practice looping through arrays and so forth, so for the two arrays you defined in your question if you want the youngest person then loop through the age array to find the location of the lowest age and then output the name from the corresponding location in the name array:
functiongetYoungest(names, ages) {
if (names.length != ages.length || names.length === 0)
return""; // stop if arrays were a different length or emptyvar i,
y,
a = Number.MAX_VALUE;
for (i=0; i < ages.length; i++) {
if (ages[i] < a) {
a = ages[i];
y = names[i];
}
}
return y;
}
varName = ['John', 'Ed', 'Sam', 'Alex', 'Mark'],
Age = [16, 25, 18, 30, 27];
console.log(getYoungest(Name, Age)); // 'John'
The above uses a
to keep track of the lowest age so far, and y
to keep the name that goes with that age, but of course you could add additional variables to simultaneously keep track of the highest age and corresponding name so far so that you could output the youngest and oldest with only one pass through the array.
(Of course something similar to the above would work on an array of objects too, if you didn't want to sort the array, or there was some other criteria you were looking for where sorting wouldn't help.)
Post a Comment for "Javascript: Manipulate Two Arrays"