最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loops - JavaScript - prevent push of certain element into an array if condition is true - Stack Overflow

programmeradmin1浏览0评论

So I have this system that iterates through a group of users and if their genre preference matches mine then they are pushed into an array of potential matches. However I want it so that I can't match with myself (i.e. the user with the same userid, in this case '324') but I want the rest of the matches to be pushed into the array.

var me = {meUserid: 324, meGenre: 'rock'};

var users = {
    user1: {userid: 276, userGenre: 'rock'},
    user2: {userid: 335, userGenre: 'jazz'},
    user3: {userid: 324, userGenre: 'rock'}, //Same userid and genre
    user4: {userid: 603, userGenre: 'rock'},
    user5: {userid: 502, userGenre: 'country'},
};

// Users array
var userProfile = [];

// Populate users array
for(var key in users) {
    userProfile.push(users[key]);
}

var potentialMatches = [];

for(var i = 0; i < userProfile.length; i++){

    // If genre matches that of another user's genre preference, push these patible users into matches array
    if(userProfile[i].userGenre == me.meGenre){

        potentialMatches.push(userProfile[i]);
    }
}

console.log(potentialMatches);

I know it will be an if statement something along the lines of:

if(meUserid == userProfile[i].userid){
   //Do something
}

but am unsure of what to make the if statement, any ideas?

The result should look something like:

potentialMatches = [{user1: {userid: 276, userGenre: 'rock'}}, {user4: {userid: 603, userGenre: 'rock'}}]

Thanks!

So I have this system that iterates through a group of users and if their genre preference matches mine then they are pushed into an array of potential matches. However I want it so that I can't match with myself (i.e. the user with the same userid, in this case '324') but I want the rest of the matches to be pushed into the array.

var me = {meUserid: 324, meGenre: 'rock'};

var users = {
    user1: {userid: 276, userGenre: 'rock'},
    user2: {userid: 335, userGenre: 'jazz'},
    user3: {userid: 324, userGenre: 'rock'}, //Same userid and genre
    user4: {userid: 603, userGenre: 'rock'},
    user5: {userid: 502, userGenre: 'country'},
};

// Users array
var userProfile = [];

// Populate users array
for(var key in users) {
    userProfile.push(users[key]);
}

var potentialMatches = [];

for(var i = 0; i < userProfile.length; i++){

    // If genre matches that of another user's genre preference, push these patible users into matches array
    if(userProfile[i].userGenre == me.meGenre){

        potentialMatches.push(userProfile[i]);
    }
}

console.log(potentialMatches);

I know it will be an if statement something along the lines of:

if(meUserid == userProfile[i].userid){
   //Do something
}

but am unsure of what to make the if statement, any ideas?

The result should look something like:

potentialMatches = [{user1: {userid: 276, userGenre: 'rock'}}, {user4: {userid: 603, userGenre: 'rock'}}]

Thanks!

Share Improve this question edited May 8, 2016 at 18:11 mellows asked May 8, 2016 at 18:02 mellowsmellows 3331 gold badge11 silver badges28 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Just add this to if statement

for (var i = 0; i < userProfile.length; i++) {
  if (userProfile[i].userGenre == me.meGenre && userProfile[i].userid != me.meUserid) {
     potentialMatches.push(userProfile[i]);
   }
}

You could use Array#filter and build the array with Array#map.

var me = { meUserid: 324, meGenre: 'rock' },
    users = { user1: { userid: 276, userGenre: 'rock' }, user2: { userid: 335, userGenre: 'jazz' }, user3: { userid: 324, userGenre: 'rock' }, user5: { userid: 502, userGenre: 'country' } },
    result = Object.keys(users).filter(function (k) {
        return users[k].userid === me.meUserid && users[k].userGenre === me.meGenre;
    }).map(function (k) { return users[k] });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Just add this inside the current loop :

if(userProfile[i].userid != me.meUserid){
    potentialMatches.push(userProfile[i]);
}

OR you may also add it with the other if loop :

if (userProfile[i].userGenre == me.meGenre && userProfile[i].userid != me.meUserid) {
     potentialMatches.push(userProfile[i]);
   }
发布评论

评论列表(0)

  1. 暂无评论