简体   繁体   中英

Convert array of objects to array of arrays?

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 ?

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; } 

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]) ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM