How to merge and return new array from object in es6How can I merge properties of two JavaScript objects...
How can I raise concerns with a new DM about XP splitting?
Indicating multiple different modes of speech (fantasy language or telepathy)
What do you call the infoboxes with text and sometimes images on the side of a page we find in textbooks?
Is there an wasy way to program in Tikz something like the one in the image?
Invariance of results when scaling explanatory variables in logistic regression, is there a proof?
Stereotypical names
Why Were Madagascar and New Zealand Discovered So Late?
Pronouncing Homer as in modern Greek
How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?
Calculating the number of days between 2 dates in Excel
Proving by induction of n. Is this correct until this point?
Why are on-board computers allowed to change controls without notifying the pilots?
Should a half Jewish man be discouraged from marrying a Jewess?
Bob has never been a M before
Meta programming: Declare a new struct on the fly
How can I successfully establish a nationwide combat training program for a large country?
Can a Gentile theist be saved?
How do I repair my stair bannister?
Java - What do constructor type arguments mean when placed *before* the type?
Simulating a probability of 1 of 2^N with less than N random bits
How do I rename a LINUX host without needing to reboot for the rename to take effect?
What is the term when two people sing in harmony, but they aren't singing the same notes?
What does 사자 in this picture means?
Can somebody explain Brexit in a few child-proof sentences?
How to merge and return new array from object in es6
How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do I correctly clone a JavaScript object?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?
Suppose there are two objects.
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
and the result
{
'1-1':[
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
],
'1-2':[
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
],
'2-1':[
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' },
]
}
Basically, I want to group the data.
I use includes
to check if the item from b
to match the id from a
. Then construct the new array.
This is my attempt(fiddle):
return b.map(item => a.map(jtem => {
if(jtem.id.includes(item)){
return {
[item]: jtem
}
}
}))
For somehow, it doesn't work.
and, is there a clever way to avoid the nested for
loop or map
function?
javascript ecmascript-6 ecmascript-7
add a comment |
Suppose there are two objects.
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
and the result
{
'1-1':[
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
],
'1-2':[
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
],
'2-1':[
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' },
]
}
Basically, I want to group the data.
I use includes
to check if the item from b
to match the id from a
. Then construct the new array.
This is my attempt(fiddle):
return b.map(item => a.map(jtem => {
if(jtem.id.includes(item)){
return {
[item]: jtem
}
}
}))
For somehow, it doesn't work.
and, is there a clever way to avoid the nested for
loop or map
function?
javascript ecmascript-6 ecmascript-7
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
Do you really wantincludes
? I'd recommendstartsWith
– Bergi
21 hours ago
@Bergi thx, I thinkstartWith
is better
– SPG
5 hours ago
add a comment |
Suppose there are two objects.
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
and the result
{
'1-1':[
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
],
'1-2':[
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
],
'2-1':[
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' },
]
}
Basically, I want to group the data.
I use includes
to check if the item from b
to match the id from a
. Then construct the new array.
This is my attempt(fiddle):
return b.map(item => a.map(jtem => {
if(jtem.id.includes(item)){
return {
[item]: jtem
}
}
}))
For somehow, it doesn't work.
and, is there a clever way to avoid the nested for
loop or map
function?
javascript ecmascript-6 ecmascript-7
Suppose there are two objects.
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
and the result
{
'1-1':[
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
],
'1-2':[
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
],
'2-1':[
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' },
]
}
Basically, I want to group the data.
I use includes
to check if the item from b
to match the id from a
. Then construct the new array.
This is my attempt(fiddle):
return b.map(item => a.map(jtem => {
if(jtem.id.includes(item)){
return {
[item]: jtem
}
}
}))
For somehow, it doesn't work.
and, is there a clever way to avoid the nested for
loop or map
function?
javascript ecmascript-6 ecmascript-7
javascript ecmascript-6 ecmascript-7
edited 23 hours ago
SPG
asked 23 hours ago
SPGSPG
2,439103359
2,439103359
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
Do you really wantincludes
? I'd recommendstartsWith
– Bergi
21 hours ago
@Bergi thx, I thinkstartWith
is better
– SPG
5 hours ago
add a comment |
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
Do you really wantincludes
? I'd recommendstartsWith
– Bergi
21 hours ago
@Bergi thx, I thinkstartWith
is better
– SPG
5 hours ago
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
Do you really want
includes
? I'd recommend startsWith
– Bergi
21 hours ago
Do you really want
includes
? I'd recommend startsWith
– Bergi
21 hours ago
@Bergi thx, I think
startWith
is better– SPG
5 hours ago
@Bergi thx, I think
startWith
is better– SPG
5 hours ago
add a comment |
1 Answer
1
active
oldest
votes
You can do that in following steps:
Apply
reduce()
on the arrayb
During each iteration use
filter()
on the the arraya
- Get all the items from
a
which starts with item ofb
usingString.prototype.startsWith()
- At last set it as property of the
ac
and returnac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by @Falco is the comments that It would be better to scan over the a
once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith
is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For biga
and smallb
I would probably go witha.reduce(...)
because of locality and only scan over the big array once.
– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
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%2f55332644%2fhow-to-merge-and-return-new-array-from-object-in-es6%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
You can do that in following steps:
Apply
reduce()
on the arrayb
During each iteration use
filter()
on the the arraya
- Get all the items from
a
which starts with item ofb
usingString.prototype.startsWith()
- At last set it as property of the
ac
and returnac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by @Falco is the comments that It would be better to scan over the a
once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith
is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For biga
and smallb
I would probably go witha.reduce(...)
because of locality and only scan over the big array once.
– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
add a comment |
You can do that in following steps:
Apply
reduce()
on the arrayb
During each iteration use
filter()
on the the arraya
- Get all the items from
a
which starts with item ofb
usingString.prototype.startsWith()
- At last set it as property of the
ac
and returnac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by @Falco is the comments that It would be better to scan over the a
once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith
is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For biga
and smallb
I would probably go witha.reduce(...)
because of locality and only scan over the big array once.
– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
add a comment |
You can do that in following steps:
Apply
reduce()
on the arrayb
During each iteration use
filter()
on the the arraya
- Get all the items from
a
which starts with item ofb
usingString.prototype.startsWith()
- At last set it as property of the
ac
and returnac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by @Falco is the comments that It would be better to scan over the a
once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith
is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
You can do that in following steps:
Apply
reduce()
on the arrayb
During each iteration use
filter()
on the the arraya
- Get all the items from
a
which starts with item ofb
usingString.prototype.startsWith()
- At last set it as property of the
ac
and returnac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by @Falco is the comments that It would be better to scan over the a
once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith
is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}
edited 14 hours ago
answered 23 hours ago
Maheer AliMaheer Ali
7,451619
7,451619
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For biga
and smallb
I would probably go witha.reduce(...)
because of locality and only scan over the big array once.
– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
add a comment |
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For biga
and smallb
I would probably go witha.reduce(...)
because of locality and only scan over the big array once.
– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
1
1
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
While it is specifically said in the question that the op wants to use es6, and that IE don't support es6 features, I just want to mention that startsWith() don't work in IE (while reduce, filter, and setting a property of an object is totally fine if IE > 9) and if someone wants to do the same thing that startsWith do, they can implment their own with some substring :)
– Neyt
20 hours ago
For big
a
and small b
I would probably go with a.reduce(...)
because of locality and only scan over the big array once.– Falco
18 hours ago
For big
a
and small b
I would probably go with a.reduce(...)
because of locality and only scan over the big array once.– Falco
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
@Falco Thanks for suggestion I updated.
– Maheer Ali
18 hours ago
1
1
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@Neyt Thanks for suggestion I updated
– Maheer Ali
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
@MaheerAli Thank you - here is a benchmark comparing the two :-) jsbench.me/dfjtoadysr/1
– Falco
18 hours ago
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%2f55332644%2fhow-to-merge-and-return-new-array-from-object-in-es6%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
Shouldn't the result be an object?
– Jack Bashford
23 hours ago
@JackBashford Hey man, sry, u r right, I just updated it.
– SPG
23 hours ago
Do you really want
includes
? I'd recommendstartsWith
– Bergi
21 hours ago
@Bergi thx, I think
startWith
is better– SPG
5 hours ago