1

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 ?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.