admin 管理员组文章数量: 1086019
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 03 Answers
Reset to default 3Just 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]);
}
本文标签: loopsJavaScriptprevent push of certain element into an array if condition is trueStack Overflow
版权声明:本文标题:loops - JavaScript - prevent push of certain element into an array if condition is true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744056113a2525879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论