1

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?

11
  • Wait... why exactly do you not want to push things into the select_people array? Commented Oct 29, 2014 at 2:06
  • i in your code is undefined. Commented Oct 29, 2014 at 2:07
  • what is your expected output or use case? Commented Oct 29, 2014 at 2:07
  • If you're looking for conciseness, you could do var selectPeople = people.filter(function(k, i) { return nums.indexOf(i) >= 0; }); Commented Oct 29, 2014 at 2:08
  • 3
    How about var selected = nums.map(function(v) { return people[v]; }); Commented Oct 29, 2014 at 2:13

4 Answers 4

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try this out. Give me a minute or two.
It works! Thanks!!!! If you're wondering why push is not the best option. I'm using AngularJS Select2 and I'm having issues with updating the ng-model for the Select2 input field. Pushing to the ng-model variable doesn't do squat, so I have to create an entire new array.
0

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]])
}

2 Comments

Ooh.. so you want to push values in your select_people variable.. without using Array.push ?
Not push. Just assign the select_people variable all in one line without using Array.push. Sorry if I'm not explaining it well. I don't even know if it's possible.
0

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);

Comments

0
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"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.