3

I currently have the following data structure:

const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

And I need to turn it into this:

const foo = {
    1:{id:1, version:0, name:"test name A"},
    2:{id:2, version:0, name:"test name B"},
    3:{id:3, version:0, name:"test name C"}
};

The piece of code I actually have is this:

for(let i=0;len = bar.length; i< len;i++){
    foo[bar[i].id]= bar[i];
}

I've tried doing

bar.map((element,index)=>{
    const temporal = {[index]:element};
    foo = {...foo, temporal};
});

but I'm lost, any suggestions?

5
  • 5
    Why would you want an object with numeric indices? Commented Sep 26, 2017 at 16:27
  • 1
    Are you talking about using Array.map or the new ES6 Map structure? There seems to be some confusion. Because you can just do bar.reduce((m, o) => m.set(o.id, o), new Map()); if you want the latter. Array.map has been around long before ES6. Commented Sep 26, 2017 at 16:34
  • 3
    @Andy - new Map(Object.entries(bar)); Commented Sep 26, 2017 at 16:54
  • @adeneo I don't want to. It's a data structure I need to pass to an API. Commented Sep 26, 2017 at 17:05
  • 2
    @OriDrori, ha! I was just impressed with your code from earlier as you might have guessed. Kudos for keeping me on my toes. That was a great answer earlier btw. Commented Sep 26, 2017 at 17:10

5 Answers 5

4

You can use reduce() with Object.assign()

const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

var result = bar.reduce((r, e) => Object.assign(r, {[e.id]: e}), {});
console.log(result)

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

Comments

2

You could use Object.assign with Array#map and spread syntax ...

const
    bar = [{ id: 1, version: 0, name: "test name A" }, { id: 2, version: 0, name: "test name B" }, { id: 3, version: 0, name: "test name C" }],
    object = Object.assign(...bar.map(o => ({ [o.id]: o })));

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

You can use reduce, aka fold or inject in general:

const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

bar.reduce((obj, e, i) => { obj[e.id] = e; return obj}, {});

Comments

1

Another way could be to use forEach which iterates over the array, but doesn't return an array as map does:

let foo = {};
bar.forEach((el, idx) => foo[idx+1] = el)

Comments

1

Array.map returns an array, if you wanted to return an object, you could use Array.reduce instead

const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

var foo = bar.reduce( (a,b,i) => (a[i+1] = b, a), {});

console.log(foo);

If you just need to reformat the data for sending it to an API, there's no need to create true clones of the objects with Object.assign

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.