Add property to an array of objects
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
add a comment |
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
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
add a comment |
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
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
javascript arrays object underscore.js
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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"; );
@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
add a comment |
or use map
Results.map((obj) =>
obj.Active = 'false';
return obj;
)
Read the spec
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 objectResults.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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"; );
@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
add a comment |
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"; );
@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
add a comment |
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"; );
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"; );
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
add a comment |
@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
add a comment |
or use map
Results.map((obj) =>
obj.Active = 'false';
return obj;
)
Read the spec
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 objectResults.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
add a comment |
or use map
Results.map((obj) =>
obj.Active = 'false';
return obj;
)
Read the spec
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 objectResults.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
add a comment |
or use map
Results.map((obj) =>
obj.Active = 'false';
return obj;
)
Read the spec
or use map
Results.map((obj) =>
obj.Active = 'false';
return obj;
)
Read the spec
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 objectResults.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
add a comment |
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 objectResults.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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