thank_you
thank_you

Reputation: 11107

Create Array through Index of Numbers Without Pushing into Array

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?

Upvotes: 1

Views: 57

Answers (4)

Jimmy Febio
Jimmy Febio

Reputation: 1

for(x=0;x<nums.length;x++){
    alert(people[nums[x]]['name']);
    // or you can define your select_people here with people[nums[x]]
    // yes, you get people[1], people[2] and people[4]
    // but, your first people is "Adam", and people[1] is "Amalie"
}

so if you want to take first people with "nums" valued "1", just do

for(x=0;x<nums.length;x++){
    alert(people[nums[x]-1]['name']);
    // or you can define your select_people here with people[nums[x]-1]
    // yes, you get people[0], people[1] and people[3]
    // your first people is "Adam", and people[0] is "Adam"
}

Upvotes: 0

Majid Fouladpour
Majid Fouladpour

Reputation: 30252

Another method to achieve the same result.

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' },
];
var nums = [1,2,4];
var j = [];
for(var i = 0, l = nums.length; i < l; i++) {
  j.push(JSON.stringify(people[nums[i]]));
}
j = '[' + j.join(',') + ']';
var selectPeople = JSON.parse(j);
console.log(selectPeople);

Upvotes: 0

soktinpk
soktinpk

Reputation: 3888

If it's conciseness you want, then you could try:

var selectPeople = people.filter(function(k, i) { return nums.indexOf(i) >= 0; });

Similarly, you could do (I actually prefer this):

var selectPeople = nums.map(function(k) { return people[k]; });

Note: This only works in modern browsers.

However, I can't think of many scenarios where using push is not the best option.

If it is a naming conflict, you can always wrap it in a temporary function (which works in all browsers):

var selectPeople = (function() {
    var temp = [];
    for (var i = 0; i < nums.length; ++i) temp.push(people[nums[i]]);
    return temp;
})();

This essentially eliminates any naming conflicts (or, for example, conflicts where selectPeople is not a true array as it lacks a push method).

Upvotes: 1

jpcanamaque
jpcanamaque

Reputation: 1069

You have not initialized you i variable in you for loop:

This should work:

var select_people = [];

for(var i = 0; i < nums.length; i++) {
   select_people.push(people[nums[i]])
}

Upvotes: 0

Related Questions