Add new properties to object in javascript not work proper?









up vote
-1
down vote

favorite












Hi I'm getting stuck with one problem that seem so easy :
I want to add new properties to an existing object, but it's not work proper
here my code :



 challengeSearchNearBy: async function (longitude , latitude,min_distance,max_distance) 
var challengeNearBy = await Challenge.find(
location:
$near:
$geometry: type: "Point", coordinates: [longitude, latitude] ,
$minDistance: min_distance,
$maxDistance: max_distance


);//limit(30);

const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

o.challenger_club_name = challenger_club.club_name;
o.challenger_club_avatar = challenger_club.avatar;
o.conqueror_club_name = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name;
o.conqueror_club_avatar = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

console.log(o.challenger_club_name); // this line logged what I want (challenger_club_name)

return o; // but this object not contain what I want (challenger_club_name, challenger_club_avatar ...)
);
const challenges = await Promise.all(challengesPromise);
return challenges;
,









share|improve this question





















  • If all you want is o.challenger_club_name, then return o.challenger_club_name ?
    – CertainPerformance
    Nov 11 at 3:09










  • @CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
    – every Bit
    Nov 11 at 3:12










  • What does it contain instead?
    – CertainPerformance
    Nov 11 at 3:23










  • @CertainPerformance It's just contain all existing properties but not new properties which I added
    – every Bit
    Nov 11 at 3:27










  • Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
    – CertainPerformance
    Nov 11 at 3:34














up vote
-1
down vote

favorite












Hi I'm getting stuck with one problem that seem so easy :
I want to add new properties to an existing object, but it's not work proper
here my code :



 challengeSearchNearBy: async function (longitude , latitude,min_distance,max_distance) 
var challengeNearBy = await Challenge.find(
location:
$near:
$geometry: type: "Point", coordinates: [longitude, latitude] ,
$minDistance: min_distance,
$maxDistance: max_distance


);//limit(30);

const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

o.challenger_club_name = challenger_club.club_name;
o.challenger_club_avatar = challenger_club.avatar;
o.conqueror_club_name = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name;
o.conqueror_club_avatar = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

console.log(o.challenger_club_name); // this line logged what I want (challenger_club_name)

return o; // but this object not contain what I want (challenger_club_name, challenger_club_avatar ...)
);
const challenges = await Promise.all(challengesPromise);
return challenges;
,









share|improve this question





















  • If all you want is o.challenger_club_name, then return o.challenger_club_name ?
    – CertainPerformance
    Nov 11 at 3:09










  • @CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
    – every Bit
    Nov 11 at 3:12










  • What does it contain instead?
    – CertainPerformance
    Nov 11 at 3:23










  • @CertainPerformance It's just contain all existing properties but not new properties which I added
    – every Bit
    Nov 11 at 3:27










  • Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
    – CertainPerformance
    Nov 11 at 3:34












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Hi I'm getting stuck with one problem that seem so easy :
I want to add new properties to an existing object, but it's not work proper
here my code :



 challengeSearchNearBy: async function (longitude , latitude,min_distance,max_distance) 
var challengeNearBy = await Challenge.find(
location:
$near:
$geometry: type: "Point", coordinates: [longitude, latitude] ,
$minDistance: min_distance,
$maxDistance: max_distance


);//limit(30);

const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

o.challenger_club_name = challenger_club.club_name;
o.challenger_club_avatar = challenger_club.avatar;
o.conqueror_club_name = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name;
o.conqueror_club_avatar = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

console.log(o.challenger_club_name); // this line logged what I want (challenger_club_name)

return o; // but this object not contain what I want (challenger_club_name, challenger_club_avatar ...)
);
const challenges = await Promise.all(challengesPromise);
return challenges;
,









share|improve this question













Hi I'm getting stuck with one problem that seem so easy :
I want to add new properties to an existing object, but it's not work proper
here my code :



 challengeSearchNearBy: async function (longitude , latitude,min_distance,max_distance) 
var challengeNearBy = await Challenge.find(
location:
$near:
$geometry: type: "Point", coordinates: [longitude, latitude] ,
$minDistance: min_distance,
$maxDistance: max_distance


);//limit(30);

const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

o.challenger_club_name = challenger_club.club_name;
o.challenger_club_avatar = challenger_club.avatar;
o.conqueror_club_name = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name;
o.conqueror_club_avatar = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

console.log(o.challenger_club_name); // this line logged what I want (challenger_club_name)

return o; // but this object not contain what I want (challenger_club_name, challenger_club_avatar ...)
);
const challenges = await Promise.all(challengesPromise);
return challenges;
,






javascript json object properties






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 3:08









every Bit

8019




8019











  • If all you want is o.challenger_club_name, then return o.challenger_club_name ?
    – CertainPerformance
    Nov 11 at 3:09










  • @CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
    – every Bit
    Nov 11 at 3:12










  • What does it contain instead?
    – CertainPerformance
    Nov 11 at 3:23










  • @CertainPerformance It's just contain all existing properties but not new properties which I added
    – every Bit
    Nov 11 at 3:27










  • Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
    – CertainPerformance
    Nov 11 at 3:34
















  • If all you want is o.challenger_club_name, then return o.challenger_club_name ?
    – CertainPerformance
    Nov 11 at 3:09










  • @CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
    – every Bit
    Nov 11 at 3:12










  • What does it contain instead?
    – CertainPerformance
    Nov 11 at 3:23










  • @CertainPerformance It's just contain all existing properties but not new properties which I added
    – every Bit
    Nov 11 at 3:27










  • Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
    – CertainPerformance
    Nov 11 at 3:34















If all you want is o.challenger_club_name, then return o.challenger_club_name ?
– CertainPerformance
Nov 11 at 3:09




If all you want is o.challenger_club_name, then return o.challenger_club_name ?
– CertainPerformance
Nov 11 at 3:09












@CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
– every Bit
Nov 11 at 3:12




@CertainPerformance I want object o within new properties such as : challenger_club_name, challenger_club_avatar ...
– every Bit
Nov 11 at 3:12












What does it contain instead?
– CertainPerformance
Nov 11 at 3:23




What does it contain instead?
– CertainPerformance
Nov 11 at 3:23












@CertainPerformance It's just contain all existing properties but not new properties which I added
– every Bit
Nov 11 at 3:27




@CertainPerformance It's just contain all existing properties but not new properties which I added
– every Bit
Nov 11 at 3:27












Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
– CertainPerformance
Nov 11 at 3:34




Seems very odd, if the console.log logs the proper value, but the returned object doesn't contain that property? Might be a problem in the consumer of the whole function
– CertainPerformance
Nov 11 at 3:34












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Return new objects inside your map.



const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

return
...o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar,
conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

);





share|improve this answer




















  • yeah, thank ! it's work :)
    – every Bit
    Nov 11 at 4:13










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245509%2fadd-new-properties-to-object-in-javascript-not-work-proper%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Return new objects inside your map.



const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

return
...o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar,
conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

);





share|improve this answer




















  • yeah, thank ! it's work :)
    – every Bit
    Nov 11 at 4:13














up vote
1
down vote



accepted










Return new objects inside your map.



const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

return
...o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar,
conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

);





share|improve this answer




















  • yeah, thank ! it's work :)
    – every Bit
    Nov 11 at 4:13












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Return new objects inside your map.



const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

return
...o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar,
conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

);





share|improve this answer












Return new objects inside your map.



const challengesPromise = challengeNearBy.map(async function(o)
const challenger_club = await Club.findById(o.challenger_club_id);
const conqueror_club = await Club.findById(o.conqueror_club_id);

return
...o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar,
conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 4:09









Ty Kroll

1,024821




1,024821











  • yeah, thank ! it's work :)
    – every Bit
    Nov 11 at 4:13
















  • yeah, thank ! it's work :)
    – every Bit
    Nov 11 at 4:13















yeah, thank ! it's work :)
– every Bit
Nov 11 at 4:13




yeah, thank ! it's work :)
– every Bit
Nov 11 at 4:13

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245509%2fadd-new-properties-to-object-in-javascript-not-work-proper%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully