how can I group array of values inside of an object to another array of objectHow do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How do I test for an empty JavaScript object?How do I loop through or enumerate a JavaScript object?How do I correctly clone a JavaScript object?How do you check if a variable is an array in JavaScript?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?
Why do Radio Buttons not fill the entire outer circle?
How to get directions in deep space?
Determining multivariate least squares with constraint
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?
How were servants to the Kaiser of Imperial Germany treated and where may I find more information on them
Difference between shutdown options
What should be the ideal length of sentences in a blog post for ease of reading?
Personal or impersonal in a technical resume
Make a Bowl of Alphabet Soup
Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?
Echo with obfuscation
"Oh no!" in Latin
Why is participating in the European Parliamentary elections used as a threat?
Should I assume I have passed probation?
Check if object is null and return null
Isometric embedding of a genus g surface
Origin of pigs as a species
Can I cause damage to electrical appliances by unplugging them when they are turned on?
Can I say "fingers" when referring to toes?
Should I warn a new PhD Student?
How do you justify more code being written by following clean code practices?
If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?
What happens if I try to grapple mirror image?
how can I group array of values inside of an object to another array of object
How do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How do I test for an empty JavaScript object?How do I loop through or enumerate a JavaScript object?How do I correctly clone a JavaScript object?How do you check if a variable is an array in JavaScript?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?
I have an object with array of values
firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']},
Now i have an empty object with array
const newData = newData.ppl = []
how can I have this result in ppl array like: ppl = [firstName: 'John', lastName: 'Doe', firstName: 'Mike', lastName: 'Smith']
javascript arrays json reactjs ecmascript-6
add a comment |
I have an object with array of values
firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']},
Now i have an empty object with array
const newData = newData.ppl = []
how can I have this result in ppl array like: ppl = [firstName: 'John', lastName: 'Doe', firstName: 'Mike', lastName: 'Smith']
javascript arrays json reactjs ecmascript-6
add a comment |
I have an object with array of values
firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']},
Now i have an empty object with array
const newData = newData.ppl = []
how can I have this result in ppl array like: ppl = [firstName: 'John', lastName: 'Doe', firstName: 'Mike', lastName: 'Smith']
javascript arrays json reactjs ecmascript-6
I have an object with array of values
firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']},
Now i have an empty object with array
const newData = newData.ppl = []
how can I have this result in ppl array like: ppl = [firstName: 'John', lastName: 'Doe', firstName: 'Mike', lastName: 'Smith']
javascript arrays json reactjs ecmascript-6
javascript arrays json reactjs ecmascript-6
asked Mar 7 at 21:51
IrfanIrfan
3916
3916
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
A quick implementation with Object.entries, reduce, and forEach:
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);Explanation:
- Use
Object.entriesto get turn the object into an array of key-value pairs. - Use
.reduceto iteratively build up the result array. - Use
.forEachto loop over the array of values obtained from (1), pushing an empty object to the array (push()) if the result array isn't long enough to hold all the items in the current array of values
add a comment |
Iterate the firstName with Array.forEach(), and take the first name. Use the index to take the respective last name from the lastName array. Push a new object with the first and last names to the ppl array.
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);add a comment |
You could get the entries and assign the properties to the same index as the array's values.
This works for any arbitrary count of properties.
var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
using a map.
let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);add a comment |
You can do it with Array.reduce(), Object.values() and Object.entries() like this:
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);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%2f55053381%2fhow-can-i-group-array-of-values-inside-of-an-object-to-another-array-of-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
A quick implementation with Object.entries, reduce, and forEach:
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);Explanation:
- Use
Object.entriesto get turn the object into an array of key-value pairs. - Use
.reduceto iteratively build up the result array. - Use
.forEachto loop over the array of values obtained from (1), pushing an empty object to the array (push()) if the result array isn't long enough to hold all the items in the current array of values
add a comment |
A quick implementation with Object.entries, reduce, and forEach:
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);Explanation:
- Use
Object.entriesto get turn the object into an array of key-value pairs. - Use
.reduceto iteratively build up the result array. - Use
.forEachto loop over the array of values obtained from (1), pushing an empty object to the array (push()) if the result array isn't long enough to hold all the items in the current array of values
add a comment |
A quick implementation with Object.entries, reduce, and forEach:
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);Explanation:
- Use
Object.entriesto get turn the object into an array of key-value pairs. - Use
.reduceto iteratively build up the result array. - Use
.forEachto loop over the array of values obtained from (1), pushing an empty object to the array (push()) if the result array isn't long enough to hold all the items in the current array of values
A quick implementation with Object.entries, reduce, and forEach:
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);Explanation:
- Use
Object.entriesto get turn the object into an array of key-value pairs. - Use
.reduceto iteratively build up the result array. - Use
.forEachto loop over the array of values obtained from (1), pushing an empty object to the array (push()) if the result array isn't long enough to hold all the items in the current array of values
const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);const srcData =
firstName: ['John', 'Mike'],
lastName: ['Doe', 'Smith']
;
const ppl = Object
.entries(srcData)
.reduce((acc, [key, values]) =>
values.forEach((val, i) =>
if (i >= acc.length)
acc.push();
acc[i][key] = val;
);
return acc;
, []);
console.log(ppl);edited Mar 7 at 22:07
answered Mar 7 at 22:01
p.s.w.gp.s.w.g
119k19214256
119k19214256
add a comment |
add a comment |
Iterate the firstName with Array.forEach(), and take the first name. Use the index to take the respective last name from the lastName array. Push a new object with the first and last names to the ppl array.
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);add a comment |
Iterate the firstName with Array.forEach(), and take the first name. Use the index to take the respective last name from the lastName array. Push a new object with the first and last names to the ppl array.
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);add a comment |
Iterate the firstName with Array.forEach(), and take the first name. Use the index to take the respective last name from the lastName array. Push a new object with the first and last names to the ppl array.
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);Iterate the firstName with Array.forEach(), and take the first name. Use the index to take the respective last name from the lastName array. Push a new object with the first and last names to the ppl array.
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'];
const newData = ppl: [] ;
data.firstName.forEach((firstName, i) =>
newData.ppl.push(
firstName,
lastName: data.lastName[i]
)
);
console.log(newData);answered Mar 7 at 21:55
Ori DroriOri Drori
80.3k138897
80.3k138897
add a comment |
add a comment |
You could get the entries and assign the properties to the same index as the array's values.
This works for any arbitrary count of properties.
var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could get the entries and assign the properties to the same index as the array's values.
This works for any arbitrary count of properties.
var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could get the entries and assign the properties to the same index as the array's values.
This works for any arbitrary count of properties.
var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; You could get the entries and assign the properties to the same index as the array's values.
This works for any arbitrary count of properties.
var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; var data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ,
result = Object
.entries(data)
.reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || )[k] = v), r), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; answered Mar 7 at 22:01
Nina ScholzNina Scholz
192k15104177
192k15104177
add a comment |
add a comment |
using a map.
let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);add a comment |
using a map.
let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);add a comment |
using a map.
let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);using a map.
let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);let master = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']
const newData = ;
newData.ppl = master.firstName.map((f,i) =>
return firstName: f, lastName: master.lastName[i];
);
console.log(newData.ppl);answered Mar 7 at 22:05
BibbertyBibberty
2,0141315
2,0141315
add a comment |
add a comment |
You can do it with Array.reduce(), Object.values() and Object.entries() like this:
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);add a comment |
You can do it with Array.reduce(), Object.values() and Object.entries() like this:
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);add a comment |
You can do it with Array.reduce(), Object.values() and Object.entries() like this:
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);You can do it with Array.reduce(), Object.values() and Object.entries() like this:
const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);const data = firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] ;
const ppl = Object.entries(data).reduce((acc, [key, arr]) =>
arr.forEach((v, i) => (acc[i] = (acc[i] , );
const newData = ppl: Object.values(ppl) ;
console.log(newData);answered Mar 7 at 22:08
jo_vajo_va
7,2962829
7,2962829
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%2f55053381%2fhow-can-i-group-array-of-values-inside-of-an-object-to-another-array-of-object%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