Access object inside array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have an object and I want to access the value of types as a property to another object like a.Fax, a.Email, a.Call etc. But I have written a function down below and it pushes the values to a.ttt. So instead of ttt I want to access each value inside types i.e Fax, Email, Call, Text which is stored in ttt.
export const ActionTypes2 = [
{ value: "OtherLawyer", label: "Other Lawyer", types: ["Fax", "Email", "Call", "Text"] },
{ value: "OtherClerk", label: "Other Clerk", types: ["Fax", "Email", "Call", "Text"] },
{ value: "BrokerageAgent", label: "Brokerage Agent", types: ["Fax", "Email", "Call", "Text"] },
{ value: "Lawyer", label: "Lawyer", types: ["Email", "Call", "Text"] },
{ value: "Clerk", label: "Clerk", types: ["Email", "Call", "Text"] },
{ value: "Client", label: "Client", types: ["Email", "Call", "Text"] },
{ value: "ListingAgent", label: "Listing Agent", types: ["Email", "Call", "Text"] },
{ value: "CooperatingAgent", label: "Cooperating Agent", types: ["Email", "Call", "Text"] }
]
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(t);
// console.log(ttt);
})
})
console.log(f);
}
javascript

add a comment |
I have an object and I want to access the value of types as a property to another object like a.Fax, a.Email, a.Call etc. But I have written a function down below and it pushes the values to a.ttt. So instead of ttt I want to access each value inside types i.e Fax, Email, Call, Text which is stored in ttt.
export const ActionTypes2 = [
{ value: "OtherLawyer", label: "Other Lawyer", types: ["Fax", "Email", "Call", "Text"] },
{ value: "OtherClerk", label: "Other Clerk", types: ["Fax", "Email", "Call", "Text"] },
{ value: "BrokerageAgent", label: "Brokerage Agent", types: ["Fax", "Email", "Call", "Text"] },
{ value: "Lawyer", label: "Lawyer", types: ["Email", "Call", "Text"] },
{ value: "Clerk", label: "Clerk", types: ["Email", "Call", "Text"] },
{ value: "Client", label: "Client", types: ["Email", "Call", "Text"] },
{ value: "ListingAgent", label: "Listing Agent", types: ["Email", "Call", "Text"] },
{ value: "CooperatingAgent", label: "Cooperating Agent", types: ["Email", "Call", "Text"] }
]
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(t);
// console.log(ttt);
})
})
console.log(f);
}
javascript

3
Please provide example result array
– styopdev
Jan 3 at 7:11
Your code should already work but you needif (!f[ttt])
,f[ttt] = ;
andf[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).
– HMR
Jan 3 at 8:26
add a comment |
I have an object and I want to access the value of types as a property to another object like a.Fax, a.Email, a.Call etc. But I have written a function down below and it pushes the values to a.ttt. So instead of ttt I want to access each value inside types i.e Fax, Email, Call, Text which is stored in ttt.
export const ActionTypes2 = [
{ value: "OtherLawyer", label: "Other Lawyer", types: ["Fax", "Email", "Call", "Text"] },
{ value: "OtherClerk", label: "Other Clerk", types: ["Fax", "Email", "Call", "Text"] },
{ value: "BrokerageAgent", label: "Brokerage Agent", types: ["Fax", "Email", "Call", "Text"] },
{ value: "Lawyer", label: "Lawyer", types: ["Email", "Call", "Text"] },
{ value: "Clerk", label: "Clerk", types: ["Email", "Call", "Text"] },
{ value: "Client", label: "Client", types: ["Email", "Call", "Text"] },
{ value: "ListingAgent", label: "Listing Agent", types: ["Email", "Call", "Text"] },
{ value: "CooperatingAgent", label: "Cooperating Agent", types: ["Email", "Call", "Text"] }
]
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(t);
// console.log(ttt);
})
})
console.log(f);
}
javascript

I have an object and I want to access the value of types as a property to another object like a.Fax, a.Email, a.Call etc. But I have written a function down below and it pushes the values to a.ttt. So instead of ttt I want to access each value inside types i.e Fax, Email, Call, Text which is stored in ttt.
export const ActionTypes2 = [
{ value: "OtherLawyer", label: "Other Lawyer", types: ["Fax", "Email", "Call", "Text"] },
{ value: "OtherClerk", label: "Other Clerk", types: ["Fax", "Email", "Call", "Text"] },
{ value: "BrokerageAgent", label: "Brokerage Agent", types: ["Fax", "Email", "Call", "Text"] },
{ value: "Lawyer", label: "Lawyer", types: ["Email", "Call", "Text"] },
{ value: "Clerk", label: "Clerk", types: ["Email", "Call", "Text"] },
{ value: "Client", label: "Client", types: ["Email", "Call", "Text"] },
{ value: "ListingAgent", label: "Listing Agent", types: ["Email", "Call", "Text"] },
{ value: "CooperatingAgent", label: "Cooperating Agent", types: ["Email", "Call", "Text"] }
]
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(t);
// console.log(ttt);
})
})
console.log(f);
}
javascript

javascript

asked Jan 3 at 7:03
Ammar HussainAmmar Hussain
1067
1067
3
Please provide example result array
– styopdev
Jan 3 at 7:11
Your code should already work but you needif (!f[ttt])
,f[ttt] = ;
andf[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).
– HMR
Jan 3 at 8:26
add a comment |
3
Please provide example result array
– styopdev
Jan 3 at 7:11
Your code should already work but you needif (!f[ttt])
,f[ttt] = ;
andf[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).
– HMR
Jan 3 at 8:26
3
3
Please provide example result array
– styopdev
Jan 3 at 7:11
Please provide example result array
– styopdev
Jan 3 at 7:11
Your code should already work but you need
if (!f[ttt])
, f[ttt] = ;
and f[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).– HMR
Jan 3 at 8:26
Your code should already work but you need
if (!f[ttt])
, f[ttt] = ;
and f[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).– HMR
Jan 3 at 8:26
add a comment |
3 Answers
3
active
oldest
votes
How about below,
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(tt.types);
})
console.log(f.ttt); }
add a comment |
you should replace f.ttt with f[ttt] as follows
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f[ttt]) {
f[ttt] = ;
}
f[ttt].push(t);
// console.log(ttt);
})
})
console.log(f);
}
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
add a comment |
You can reduce the array to an object (note that SO prints the console a bit strange, press F12 and check out the console for the actual result):
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
Array.prototype.map is used to convert an array of 'T to an array of 'U or even produce a new array of the same type but doubled (for example [1,2,3].map(x=>x*2)
). The function passed to map should be a pure function so it should not use or mutate something defined outside the function or change the array being mapped.
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%2f54017724%2faccess-object-inside-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about below,
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(tt.types);
})
console.log(f.ttt); }
add a comment |
How about below,
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(tt.types);
})
console.log(f.ttt); }
add a comment |
How about below,
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(tt.types);
})
console.log(f.ttt); }
How about below,
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
if (!f.ttt) {
f.ttt = ;
}
f.ttt.push(tt.types);
})
console.log(f.ttt); }
edited Jan 3 at 7:58
answered Jan 3 at 7:52


Jitendra G2Jitendra G2
803612
803612
add a comment |
add a comment |
you should replace f.ttt with f[ttt] as follows
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f[ttt]) {
f[ttt] = ;
}
f[ttt].push(t);
// console.log(ttt);
})
})
console.log(f);
}
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
add a comment |
you should replace f.ttt with f[ttt] as follows
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f[ttt]) {
f[ttt] = ;
}
f[ttt].push(t);
// console.log(ttt);
})
})
console.log(f);
}
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
add a comment |
you should replace f.ttt with f[ttt] as follows
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f[ttt]) {
f[ttt] = ;
}
f[ttt].push(t);
// console.log(ttt);
})
})
console.log(f);
}
you should replace f.ttt with f[ttt] as follows
getActionTypes() {
let t = ActionTypes2;
let f = <any>{};
t.map((tt: any) => {
tt.types.map((ttt: any) => {
if (!f[ttt]) {
f[ttt] = ;
}
f[ttt].push(t);
// console.log(ttt);
})
})
console.log(f);
}
answered Jan 3 at 8:09
Mohit PatilMohit Patil
8619
8619
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
add a comment |
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
Method passed to map should be a pure function, if you mutate or use something defined outside of the function you should probably use forEach instead. However; this could be more easily accomplished using reduce.
– HMR
Jan 3 at 8:18
add a comment |
You can reduce the array to an object (note that SO prints the console a bit strange, press F12 and check out the console for the actual result):
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
Array.prototype.map is used to convert an array of 'T to an array of 'U or even produce a new array of the same type but doubled (for example [1,2,3].map(x=>x*2)
). The function passed to map should be a pure function so it should not use or mutate something defined outside the function or change the array being mapped.
add a comment |
You can reduce the array to an object (note that SO prints the console a bit strange, press F12 and check out the console for the actual result):
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
Array.prototype.map is used to convert an array of 'T to an array of 'U or even produce a new array of the same type but doubled (for example [1,2,3].map(x=>x*2)
). The function passed to map should be a pure function so it should not use or mutate something defined outside the function or change the array being mapped.
add a comment |
You can reduce the array to an object (note that SO prints the console a bit strange, press F12 and check out the console for the actual result):
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
Array.prototype.map is used to convert an array of 'T to an array of 'U or even produce a new array of the same type but doubled (for example [1,2,3].map(x=>x*2)
). The function passed to map should be a pure function so it should not use or mutate something defined outside the function or change the array being mapped.
You can reduce the array to an object (note that SO prints the console a bit strange, press F12 and check out the console for the actual result):
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
Array.prototype.map is used to convert an array of 'T to an array of 'U or even produce a new array of the same type but doubled (for example [1,2,3].map(x=>x*2)
). The function passed to map should be a pure function so it should not use or mutate something defined outside the function or change the array being mapped.
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
const data = [{"value":"OtherLawyer","label":"Other Lawyer","types":["Fax","Email","Call","Text"]},{"value":"OtherClerk","label":"Other Clerk","types":["Fax","Email","Call","Text"]},{"value":"BrokerageAgent","label":"Brokerage Agent","types":["Fax","Email","Call","Text"]},{"value":"Lawyer","label":"Lawyer","types":["Email","Call","Text"]},{"value":"Clerk","label":"Clerk","types":["Email","Call","Text"]},{"value":"Client","label":"Client","types":["Email","Call","Text"]},{"value":"ListingAgent","label":"Listing Agent","types":["Email","Call","Text"]},{"value":"CooperatingAgent","label":"Cooperating Agent","types":["Email","Call","Text"]}];
const groupByType = (data) =>
data.reduce(
(result, { types, ...rest }) =>
types.reduce((result, type) => {
result[type] = result[type] || ;
result[type].push(rest);
return result;
}, result),
{},
);
console.log(groupByType(data));
answered Jan 3 at 8:13
HMRHMR
14.1k1138102
14.1k1138102
add a comment |
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%2f54017724%2faccess-object-inside-array%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
3
Please provide example result array
– styopdev
Jan 3 at 7:11
Your code should already work but you need
if (!f[ttt])
,f[ttt] = ;
andf[ttt].push(t);
. I would also advice to not use map but forEach, map is used to return a new array and usually does not mutate something or use reduce as in my answer as that's used to return a new value from an array of values (like sum an array of numbers).– HMR
Jan 3 at 8:26