Add property to an array of objects










31















I have an array of objects as shown below



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
1:Object
id=2
name:'david'


I want to add one more property named Active to each element of this array of Objects.



The final outcome should be as follows.



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
Active: "false"
1:Object
id=2
name:'david'
Active: "false"


Can someone please let me know how to achieve this.










share|improve this question



















  • 4





    Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

    – JJJ
    Aug 12 '16 at 16:58















31















I have an array of objects as shown below



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
1:Object
id=2
name:'david'


I want to add one more property named Active to each element of this array of Objects.



The final outcome should be as follows.



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
Active: "false"
1:Object
id=2
name:'david'
Active: "false"


Can someone please let me know how to achieve this.










share|improve this question



















  • 4





    Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

    – JJJ
    Aug 12 '16 at 16:58













31












31








31


6






I have an array of objects as shown below



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
1:Object
id=2
name:'david'


I want to add one more property named Active to each element of this array of Objects.



The final outcome should be as follows.



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
Active: "false"
1:Object
id=2
name:'david'
Active: "false"


Can someone please let me know how to achieve this.










share|improve this question
















I have an array of objects as shown below



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
1:Object
id=2
name:'david'


I want to add one more property named Active to each element of this array of Objects.



The final outcome should be as follows.



Object Results:Array[2]
Results:Array[2]
[0-1]
0:Object
id=1
name: "Rick"
Active: "false"
1:Object
id=2
name:'david'
Active: "false"


Can someone please let me know how to achieve this.







javascript arrays object underscore.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '16 at 6:25







user663031

















asked Aug 12 '16 at 16:53









PatrickPatrick

5602720




5602720







  • 4





    Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

    – JJJ
    Aug 12 '16 at 16:58












  • 4





    Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

    – JJJ
    Aug 12 '16 at 16:58







4




4





Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

– JJJ
Aug 12 '16 at 16:58





Loop through the array. Add properties to each array element while looping. Which part do you not know how to do?

– JJJ
Aug 12 '16 at 16:58












2 Answers
2






active

oldest

votes


















61














You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.



Results.forEach(function(element) element.Active = "false"; );





share|improve this answer

























  • @tholle- it gives me synatax error near "=>"

    – Patrick
    Aug 12 '16 at 17:08












  • @Patrick Excuse me. Updated the answer.

    – Tholle
    Aug 12 '16 at 17:10











  • update your browser or node version.

    – Azarus
    Oct 3 '17 at 21:45











  • Or if you're looking for an actual bool: false (with no quotes).

    – FernandoG
    Oct 24 '18 at 17:10


















43














or use map



Results.map((obj) => 
obj.Active = 'false';
return obj;
)


Read the spec






share|improve this answer




















  • 1





    only supported in ES6

    – Amaynut
    Mar 15 '18 at 19:36






  • 3





    ES5 to be precise ;) - but babel will happily decompile it

    – sidonaldson
    Mar 16 '18 at 11:19






  • 9





    map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

    – adrianolsk
    Jul 17 '18 at 15:11







  • 1





    Great solution @adrianolsk, you should submit that as a separate answer.

    – Michael Hays
    Aug 24 '18 at 16:21










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',
autoActivateHeartbeat: false,
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%2f38922998%2fadd-property-to-an-array-of-objects%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









61














You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.



Results.forEach(function(element) element.Active = "false"; );





share|improve this answer

























  • @tholle- it gives me synatax error near "=>"

    – Patrick
    Aug 12 '16 at 17:08












  • @Patrick Excuse me. Updated the answer.

    – Tholle
    Aug 12 '16 at 17:10











  • update your browser or node version.

    – Azarus
    Oct 3 '17 at 21:45











  • Or if you're looking for an actual bool: false (with no quotes).

    – FernandoG
    Oct 24 '18 at 17:10















61














You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.



Results.forEach(function(element) element.Active = "false"; );





share|improve this answer

























  • @tholle- it gives me synatax error near "=>"

    – Patrick
    Aug 12 '16 at 17:08












  • @Patrick Excuse me. Updated the answer.

    – Tholle
    Aug 12 '16 at 17:10











  • update your browser or node version.

    – Azarus
    Oct 3 '17 at 21:45











  • Or if you're looking for an actual bool: false (with no quotes).

    – FernandoG
    Oct 24 '18 at 17:10













61












61








61







You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.



Results.forEach(function(element) element.Active = "false"; );





share|improve this answer















You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.



Results.forEach(function(element) element.Active = "false"; );






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 '18 at 12:00

























answered Aug 12 '16 at 17:05









TholleTholle

34.2k53760




34.2k53760












  • @tholle- it gives me synatax error near "=>"

    – Patrick
    Aug 12 '16 at 17:08












  • @Patrick Excuse me. Updated the answer.

    – Tholle
    Aug 12 '16 at 17:10











  • update your browser or node version.

    – Azarus
    Oct 3 '17 at 21:45











  • Or if you're looking for an actual bool: false (with no quotes).

    – FernandoG
    Oct 24 '18 at 17:10

















  • @tholle- it gives me synatax error near "=>"

    – Patrick
    Aug 12 '16 at 17:08












  • @Patrick Excuse me. Updated the answer.

    – Tholle
    Aug 12 '16 at 17:10











  • update your browser or node version.

    – Azarus
    Oct 3 '17 at 21:45











  • Or if you're looking for an actual bool: false (with no quotes).

    – FernandoG
    Oct 24 '18 at 17:10
















@tholle- it gives me synatax error near "=>"

– Patrick
Aug 12 '16 at 17:08






@tholle- it gives me synatax error near "=>"

– Patrick
Aug 12 '16 at 17:08














@Patrick Excuse me. Updated the answer.

– Tholle
Aug 12 '16 at 17:10





@Patrick Excuse me. Updated the answer.

– Tholle
Aug 12 '16 at 17:10













update your browser or node version.

– Azarus
Oct 3 '17 at 21:45





update your browser or node version.

– Azarus
Oct 3 '17 at 21:45













Or if you're looking for an actual bool: false (with no quotes).

– FernandoG
Oct 24 '18 at 17:10





Or if you're looking for an actual bool: false (with no quotes).

– FernandoG
Oct 24 '18 at 17:10













43














or use map



Results.map((obj) => 
obj.Active = 'false';
return obj;
)


Read the spec






share|improve this answer




















  • 1





    only supported in ES6

    – Amaynut
    Mar 15 '18 at 19:36






  • 3





    ES5 to be precise ;) - but babel will happily decompile it

    – sidonaldson
    Mar 16 '18 at 11:19






  • 9





    map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

    – adrianolsk
    Jul 17 '18 at 15:11







  • 1





    Great solution @adrianolsk, you should submit that as a separate answer.

    – Michael Hays
    Aug 24 '18 at 16:21















43














or use map



Results.map((obj) => 
obj.Active = 'false';
return obj;
)


Read the spec






share|improve this answer




















  • 1





    only supported in ES6

    – Amaynut
    Mar 15 '18 at 19:36






  • 3





    ES5 to be precise ;) - but babel will happily decompile it

    – sidonaldson
    Mar 16 '18 at 11:19






  • 9





    map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

    – adrianolsk
    Jul 17 '18 at 15:11







  • 1





    Great solution @adrianolsk, you should submit that as a separate answer.

    – Michael Hays
    Aug 24 '18 at 16:21













43












43








43







or use map



Results.map((obj) => 
obj.Active = 'false';
return obj;
)


Read the spec






share|improve this answer















or use map



Results.map((obj) => 
obj.Active = 'false';
return obj;
)


Read the spec







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 '18 at 10:57

























answered Jun 7 '17 at 8:55









sidonaldsonsidonaldson

13.8k73649




13.8k73649







  • 1





    only supported in ES6

    – Amaynut
    Mar 15 '18 at 19:36






  • 3





    ES5 to be precise ;) - but babel will happily decompile it

    – sidonaldson
    Mar 16 '18 at 11:19






  • 9





    map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

    – adrianolsk
    Jul 17 '18 at 15:11







  • 1





    Great solution @adrianolsk, you should submit that as a separate answer.

    – Michael Hays
    Aug 24 '18 at 16:21












  • 1





    only supported in ES6

    – Amaynut
    Mar 15 '18 at 19:36






  • 3





    ES5 to be precise ;) - but babel will happily decompile it

    – sidonaldson
    Mar 16 '18 at 11:19






  • 9





    map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

    – adrianolsk
    Jul 17 '18 at 15:11







  • 1





    Great solution @adrianolsk, you should submit that as a separate answer.

    – Michael Hays
    Aug 24 '18 at 16:21







1




1





only supported in ES6

– Amaynut
Mar 15 '18 at 19:36





only supported in ES6

– Amaynut
Mar 15 '18 at 19:36




3




3





ES5 to be precise ;) - but babel will happily decompile it

– sidonaldson
Mar 16 '18 at 11:19





ES5 to be precise ;) - but babel will happily decompile it

– sidonaldson
Mar 16 '18 at 11:19




9




9





map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

– adrianolsk
Jul 17 '18 at 15:11






map should return a new array not mutate the object, in this case forEach would be better, or use map and return a new object Results.map(obj=> ( ...obj, Active : 'false' ))

– adrianolsk
Jul 17 '18 at 15:11





1




1





Great solution @adrianolsk, you should submit that as a separate answer.

– Michael Hays
Aug 24 '18 at 16:21





Great solution @adrianolsk, you should submit that as a separate answer.

– Michael Hays
Aug 24 '18 at 16:21

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38922998%2fadd-property-to-an-array-of-objects%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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)