Determine Array include value in Another Array
I want to check if arr1 include arr2
i have try use .some and includes but it not return the right answer for me
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
my expected
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
javascript
add a comment |
I want to check if arr1 include arr2
i have try use .some and includes but it not return the right answer for me
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
my expected
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
javascript
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
So which one is it? You want the output to be just atrueorfalseOR you want to merge the arrays?
– adiga
Jan 2 at 17:36
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56
add a comment |
I want to check if arr1 include arr2
i have try use .some and includes but it not return the right answer for me
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
my expected
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
javascript
I want to check if arr1 include arr2
i have try use .some and includes but it not return the right answer for me
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
my expected
arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]
javascript
javascript
edited Jan 2 at 17:56
Phạm Minh Tân
asked Jan 2 at 17:25
Phạm Minh TânPhạm Minh Tân
295
295
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
So which one is it? You want the output to be just atrueorfalseOR you want to merge the arrays?
– adiga
Jan 2 at 17:36
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56
add a comment |
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
So which one is it? You want the output to be just atrueorfalseOR you want to merge the arrays?
– adiga
Jan 2 at 17:36
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
So which one is it? You want the output to be just a
true or false OR you want to merge the arrays?– adiga
Jan 2 at 17:36
So which one is it? You want the output to be just a
true or false OR you want to merge the arrays?– adiga
Jan 2 at 17:36
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56
add a comment |
3 Answers
3
active
oldest
votes
You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.
let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);add a comment |
Since they are not the same object just objects with same values you have to compare each value.
const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);add a comment |
You could reduce the first array and check if Name is not in the first array, then add it to the result set.
var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }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%2f54010620%2fdetermine-array-include-value-in-another-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
You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.
let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);add a comment |
You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.
let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);add a comment |
You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.
let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.
let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);answered Jan 2 at 17:29
Hassan ImamHassan Imam
11.9k31532
11.9k31532
add a comment |
add a comment |
Since they are not the same object just objects with same values you have to compare each value.
const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);add a comment |
Since they are not the same object just objects with same values you have to compare each value.
const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);add a comment |
Since they are not the same object just objects with same values you have to compare each value.
const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);Since they are not the same object just objects with same values you have to compare each value.
const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);const arr1 = [{
Name: 'John',
Age: 18
}, {
Name: 'Leona',
Age: 19
}];
const arr2 = [{
Name: 'John',
Age: 18
}];
const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
console.log(check);answered Jan 2 at 17:31
Sebastian SpeitelSebastian Speitel
4,5882727
4,5882727
add a comment |
add a comment |
You could reduce the first array and check if Name is not in the first array, then add it to the result set.
var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }add a comment |
You could reduce the first array and check if Name is not in the first array, then add it to the result set.
var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }add a comment |
You could reduce the first array and check if Name is not in the first array, then add it to the result set.
var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }You could reduce the first array and check if Name is not in the first array, then add it to the result set.
var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];
array2.reduce((r, o) => {
if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
return r;
}, array1);
console.log(array1);.as-console-wrapper { max-height: 100% !important; top: 0; }answered Jan 2 at 21:55
Nina ScholzNina Scholz
191k15103175
191k15103175
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%2f54010620%2fdetermine-array-include-value-in-another-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
Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.
– Baruch
Jan 2 at 17:29
So which one is it? You want the output to be just a
trueorfalseOR you want to merge the arrays?– adiga
Jan 2 at 17:36
please add the wanted result as well.
– Nina Scholz
Jan 2 at 17:39
i have updated , so sorry
– Phạm Minh Tân
Jan 2 at 17:56