Convert array of objects to array of arrays?
up vote
1
down vote
favorite
Given is an json object like this:
var items = [
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/'
,
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/'
,
title: 'sample 3',
image: 'http://www.lorempixel.com/400/300/'
,
title: 'sample 4',
image: 'http://www.lorempixel.com/600/600/'
,
title: 'sample 5',
image: 'http://www.lorempixel.com/400/310/'
,
title: 'sample 6',
image: 'http://www.lorempixel.com/410/300/'
,
title: 'sample 7',
image: 'http://www.lorempixel.com/500/300/'
,
title: 'sample 8',
image: 'http://www.lorempixel.com/300/300/'
,
title: 'sample 9',
image: 'http://www.lorempixel.com/450/320/'
,
title: 'sample 10',
image: 'http://www.lorempixel.com/500/400/'
];
I Want to get an array like this.
[['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]
I tried something like this:
const rows = ;
for (let i = 0; i < items.length; i++)
const element = items[i];
rows[i] = element.title;
for (let j = i; j < items.image; j++)
const el = response[j];
rows[j] = el.image;
I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?
javascript arrays json object mapping
add a comment |
up vote
1
down vote
favorite
Given is an json object like this:
var items = [
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/'
,
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/'
,
title: 'sample 3',
image: 'http://www.lorempixel.com/400/300/'
,
title: 'sample 4',
image: 'http://www.lorempixel.com/600/600/'
,
title: 'sample 5',
image: 'http://www.lorempixel.com/400/310/'
,
title: 'sample 6',
image: 'http://www.lorempixel.com/410/300/'
,
title: 'sample 7',
image: 'http://www.lorempixel.com/500/300/'
,
title: 'sample 8',
image: 'http://www.lorempixel.com/300/300/'
,
title: 'sample 9',
image: 'http://www.lorempixel.com/450/320/'
,
title: 'sample 10',
image: 'http://www.lorempixel.com/500/400/'
];
I Want to get an array like this.
[['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]
I tried something like this:
const rows = ;
for (let i = 0; i < items.length; i++)
const element = items[i];
rows[i] = element.title;
for (let j = i; j < items.image; j++)
const el = response[j];
rows[j] = el.image;
I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?
javascript arrays json object mapping
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Given is an json object like this:
var items = [
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/'
,
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/'
,
title: 'sample 3',
image: 'http://www.lorempixel.com/400/300/'
,
title: 'sample 4',
image: 'http://www.lorempixel.com/600/600/'
,
title: 'sample 5',
image: 'http://www.lorempixel.com/400/310/'
,
title: 'sample 6',
image: 'http://www.lorempixel.com/410/300/'
,
title: 'sample 7',
image: 'http://www.lorempixel.com/500/300/'
,
title: 'sample 8',
image: 'http://www.lorempixel.com/300/300/'
,
title: 'sample 9',
image: 'http://www.lorempixel.com/450/320/'
,
title: 'sample 10',
image: 'http://www.lorempixel.com/500/400/'
];
I Want to get an array like this.
[['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]
I tried something like this:
const rows = ;
for (let i = 0; i < items.length; i++)
const element = items[i];
rows[i] = element.title;
for (let j = i; j < items.image; j++)
const el = response[j];
rows[j] = el.image;
I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?
javascript arrays json object mapping
Given is an json object like this:
var items = [
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/'
,
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/'
,
title: 'sample 3',
image: 'http://www.lorempixel.com/400/300/'
,
title: 'sample 4',
image: 'http://www.lorempixel.com/600/600/'
,
title: 'sample 5',
image: 'http://www.lorempixel.com/400/310/'
,
title: 'sample 6',
image: 'http://www.lorempixel.com/410/300/'
,
title: 'sample 7',
image: 'http://www.lorempixel.com/500/300/'
,
title: 'sample 8',
image: 'http://www.lorempixel.com/300/300/'
,
title: 'sample 9',
image: 'http://www.lorempixel.com/450/320/'
,
title: 'sample 10',
image: 'http://www.lorempixel.com/500/400/'
];
I Want to get an array like this.
[['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]
I tried something like this:
const rows = ;
for (let i = 0; i < items.length; i++)
const element = items[i];
rows[i] = element.title;
for (let j = i; j < items.image; j++)
const el = response[j];
rows[j] = el.image;
I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?
javascript arrays json object mapping
javascript arrays json object mapping
edited Nov 10 at 6:46
Mohammad Usman
19.2k103653
19.2k103653
asked Nov 10 at 6:35
billyjov
658314
658314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Simply pass Object.values()
as callback to .map()
:
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
up vote
1
down vote
All you need is a simple .map
- extract the appropriate property values from the object, and return an array containing those two values:
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Simply pass Object.values()
as callback to .map()
:
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
up vote
2
down vote
accepted
Simply pass Object.values()
as callback to .map()
:
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Simply pass Object.values()
as callback to .map()
:
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
Simply pass Object.values()
as callback to .map()
:
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
let data = [
title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
];
let result = data.map(Object.values);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
answered Nov 10 at 6:37
Mohammad Usman
19.2k103653
19.2k103653
add a comment |
add a comment |
up vote
1
down vote
All you need is a simple .map
- extract the appropriate property values from the object, and return an array containing those two values:
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
add a comment |
up vote
1
down vote
All you need is a simple .map
- extract the appropriate property values from the object, and return an array containing those two values:
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
add a comment |
up vote
1
down vote
up vote
1
down vote
All you need is a simple .map
- extract the appropriate property values from the object, and return an array containing those two values:
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
All you need is a simple .map
- extract the appropriate property values from the object, and return an array containing those two values:
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];
console.log(
items.map(( title, image ) => [title, image])
);
answered Nov 10 at 6:37
CertainPerformance
69.4k143453
69.4k143453
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%2f53236592%2fconvert-array-of-objects-to-array-of-arrays%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