JS - Comparing 2 Array of Objects
I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes
Please refer to the image below
As you can see, on the 2nd record value 1:Three should be placed on col 1
This is my array of objects:
const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]
I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>
So as one of the suggestion, here's my attempt:
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)
I'm hopping my JSON will shape up like this:
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]
Let me know if anyone can come up with better solution
Thanks in advance!
javascript reactjs loops oop ecmascript-6
add a comment |
I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes
Please refer to the image below
As you can see, on the 2nd record value 1:Three should be placed on col 1
This is my array of objects:
const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]
I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>
So as one of the suggestion, here's my attempt:
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)
I'm hopping my JSON will shape up like this:
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]
Let me know if anyone can come up with better solution
Thanks in advance!
javascript reactjs loops oop ecmascript-6
add a comment |
I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes
Please refer to the image below
As you can see, on the 2nd record value 1:Three should be placed on col 1
This is my array of objects:
const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]
I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>
So as one of the suggestion, here's my attempt:
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)
I'm hopping my JSON will shape up like this:
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]
Let me know if anyone can come up with better solution
Thanks in advance!
javascript reactjs loops oop ecmascript-6
I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes
Please refer to the image below
As you can see, on the 2nd record value 1:Three should be placed on col 1
This is my array of objects:
const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]
I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>
So as one of the suggestion, here's my attempt:
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)
I'm hopping my JSON will shape up like this:
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]
Let me know if anyone can come up with better solution
Thanks in advance!
javascript reactjs loops oop ecmascript-6
javascript reactjs loops oop ecmascript-6
edited Nov 23 '18 at 7:04
asked Nov 23 '18 at 2:56
Amy Lee
134111
134111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:
const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
const cols = [{labelId:'1', value:'One'},
{labelId:'2', value:'Two'},
{labelId:'3', value:'Three'},
{labelId:'4', value:'Four'},
];
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
if(arrayOfMatch !== -1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
labelCols
will be containing something like this:
labelCols = [{labelId:'1', value:'One', label: 'test1'},
{labelId:'2', value:'Two', label: 'test2'},
{labelId:'3', value:'Three', label: ''},
{labelId:'4', value:'Four', label: ''},
];
Then, if you want to render td
elements with labels, do this:
<table>
//other codes
<tr>
{labelCols.map(item => <td>{item.label}</td>)}
</tr>
//other codes
</table>
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
add a comment |
// For comparing two array, array must be with same type of same length
var isEqualArray = labels.map((label)=>{
cols.map((col)=> label.labelId === col.labelId)
}).filer((item) => item === true)
.length === labels.length;
if (isEqualArray ) {
// Equal
}
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%2f53440147%2fjs-comparing-2-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
I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:
const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
const cols = [{labelId:'1', value:'One'},
{labelId:'2', value:'Two'},
{labelId:'3', value:'Three'},
{labelId:'4', value:'Four'},
];
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
if(arrayOfMatch !== -1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
labelCols
will be containing something like this:
labelCols = [{labelId:'1', value:'One', label: 'test1'},
{labelId:'2', value:'Two', label: 'test2'},
{labelId:'3', value:'Three', label: ''},
{labelId:'4', value:'Four', label: ''},
];
Then, if you want to render td
elements with labels, do this:
<table>
//other codes
<tr>
{labelCols.map(item => <td>{item.label}</td>)}
</tr>
//other codes
</table>
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
add a comment |
I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:
const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
const cols = [{labelId:'1', value:'One'},
{labelId:'2', value:'Two'},
{labelId:'3', value:'Three'},
{labelId:'4', value:'Four'},
];
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
if(arrayOfMatch !== -1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
labelCols
will be containing something like this:
labelCols = [{labelId:'1', value:'One', label: 'test1'},
{labelId:'2', value:'Two', label: 'test2'},
{labelId:'3', value:'Three', label: ''},
{labelId:'4', value:'Four', label: ''},
];
Then, if you want to render td
elements with labels, do this:
<table>
//other codes
<tr>
{labelCols.map(item => <td>{item.label}</td>)}
</tr>
//other codes
</table>
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
add a comment |
I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:
const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
const cols = [{labelId:'1', value:'One'},
{labelId:'2', value:'Two'},
{labelId:'3', value:'Three'},
{labelId:'4', value:'Four'},
];
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
if(arrayOfMatch !== -1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
labelCols
will be containing something like this:
labelCols = [{labelId:'1', value:'One', label: 'test1'},
{labelId:'2', value:'Two', label: 'test2'},
{labelId:'3', value:'Three', label: ''},
{labelId:'4', value:'Four', label: ''},
];
Then, if you want to render td
elements with labels, do this:
<table>
//other codes
<tr>
{labelCols.map(item => <td>{item.label}</td>)}
</tr>
//other codes
</table>
I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:
const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
const cols = [{labelId:'1', value:'One'},
{labelId:'2', value:'Two'},
{labelId:'3', value:'Three'},
{labelId:'4', value:'Four'},
];
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
if(arrayOfMatch !== -1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
labelCols
will be containing something like this:
labelCols = [{labelId:'1', value:'One', label: 'test1'},
{labelId:'2', value:'Two', label: 'test2'},
{labelId:'3', value:'Three', label: ''},
{labelId:'4', value:'Four', label: ''},
];
Then, if you want to render td
elements with labels, do this:
<table>
//other codes
<tr>
{labelCols.map(item => <td>{item.label}</td>)}
</tr>
//other codes
</table>
answered Nov 23 '18 at 3:22
Sandy Brutas
454312
454312
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
add a comment |
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Awesome! yeah that's what i want, lemme try it
– Amy Lee
Nov 23 '18 at 3:29
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!
– Amy Lee
Nov 23 '18 at 6:26
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
Edit your question above to make it clear.
– Sandy Brutas
Nov 23 '18 at 6:35
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
I just have, let me know if it's still unclear. Thanks again for your help
– Amy Lee
Nov 23 '18 at 7:05
add a comment |
// For comparing two array, array must be with same type of same length
var isEqualArray = labels.map((label)=>{
cols.map((col)=> label.labelId === col.labelId)
}).filer((item) => item === true)
.length === labels.length;
if (isEqualArray ) {
// Equal
}
add a comment |
// For comparing two array, array must be with same type of same length
var isEqualArray = labels.map((label)=>{
cols.map((col)=> label.labelId === col.labelId)
}).filer((item) => item === true)
.length === labels.length;
if (isEqualArray ) {
// Equal
}
add a comment |
// For comparing two array, array must be with same type of same length
var isEqualArray = labels.map((label)=>{
cols.map((col)=> label.labelId === col.labelId)
}).filer((item) => item === true)
.length === labels.length;
if (isEqualArray ) {
// Equal
}
// For comparing two array, array must be with same type of same length
var isEqualArray = labels.map((label)=>{
cols.map((col)=> label.labelId === col.labelId)
}).filer((item) => item === true)
.length === labels.length;
if (isEqualArray ) {
// Equal
}
edited Nov 23 '18 at 3:20
answered Nov 23 '18 at 3:12
Atul Bansode
11
11
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53440147%2fjs-comparing-2-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