I have an array
var nums = [1,2,4];
And I have another array filled with people
var people = [
{ name: 'Adam', email: '[email protected]', age: 12, country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12, country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21, country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21, country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30, country: 'Ecuador' },
];
I want to create a variable that based off using the nums variable acting as indexes to the people variable.
// With the nums array I take each value and set it as the value of the new variable
// This is my expected output. Although this is line of code is not possible since the nums variable will be unique each time the code run.
var select_people = [people[1], people[2], people[4]];
I am unable to create an empty array and then push each element into the select_people array like so.
// This will not do for me
var select_people = [];
for(var i = 0; i < nums.length; i++) {
select_people.push(people[nums[i]])
}
My question is this. How can I write this code so that I can assign the select_people variable without having to push the values into the array?
select_peoplearray?iin your code isundefined.var selectPeople = people.filter(function(k, i) { return nums.indexOf(i) >= 0; });var selected = nums.map(function(v) { return people[v]; });