0

I have an array of objects which look like this:

stuff = [
    { value: 'elevator', checked: true },
    { value: 'something', checked: false },
    { value: 'else', checked: true },
]

And I am trying to get something like this:

{
    'elevator': true,
    'something: false,
    'else': true,
}

All I can get since yesterday is an array of objects like:

[
    { 'elevator': true },
    { 'something': false },
    { 'else': true }
];

I tried with mapping on array and then using Object.assign but it's not working. I get the previous code.

1
  • Welcome to Stack Overflow! Please take the tour and read through the help center, in particular How do I ask a good question? Do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! Commented Feb 19, 2018 at 10:18

4 Answers 4

1

Use reduce

var output  = stuff.reduce( (a,c) => (a[c.value] = c.checked, a) , {} )

Demo

var stuff = [
    { value: 'elevator', checked: true },
    { value: 'something', checked: false },
    { value: 'else', checked: true },
];
var output  = stuff.reduce( (a,c) => (a[c.value] = c.checked, a) , {} )
console.log( output );

Edit

Using object.assign

stuff.reduce( (a,c) => Object.assign( {}, a, { [c.value] : c.checked })  , {} )
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try with reduce ! At lease, it's very clear working with this solution ! Thank you !
1

Iterate stuff array, So you will get each object under stuff. Then get that value that you need.

var stuff = [
   { value: 'elevator', checked: true },
   { value: 'something', checked: false },
   { value: 'else', checked: true },
];

var obj = {};
for( var i=0; i<stuff.length; i++) {
  obj[stuff[i]['value']] = stuff[i]['checked'];
}
console.log(obj);

Comments

0

You can reduce function to make an single object from the given array. Just add your logic inside it. In this case, value to the property name and checked to it's value.

const stuff = [
    { value: 'elevator', checked: true },
    { value: 'something', checked: false },
    { value: 'else', checked: true },
]

const mapped = stuff.reduce((obj, item) =>  (obj[item.value] = item.checked, obj), {});

console.log(mapped);

Comments

0

If you can use ES6, you can do it with Object.assign, array.prototype.map, some destructuring, object litteral dynamic key and spread operator:

var stuff = [
    { value: 'elevator', checked: true },
    { value: 'something', checked: false },
    { value: 'else', checked: true },
];
var result = Object.assign({}, ...stuff.map(({value, checked}) => ({[value]: checked})));
console.log(result);

1 Comment

Ok thx, It works great, I missed a level I guess and that's why I get the object I didn't want. Thank you mate!

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.