最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert a dictionary into an array keeping the dictionary keys - Stack Overflow

programmeradmin5浏览0评论

I'm trying to find a more elegant way of converting a dictionary of dictionaries to an array of dictionaries and keeping the information of the keys of the original dictionary.

In the case of

let data= { boss: { name:"Peter", phone:"123"},
            minion: { name:"Bob", phone:"456"},
            slave: { name:"Pat", phone: "789"} 
          }

I want to e up with something that gives me

output = [ { role:"boss", name:"Peter", phone:"123"},
           { role:"minion", name:"Bob", phone:"456"},
           { role:"slave", name:"Pat",  phone:"789"}
         ]

My solution goes by using the Object.keys method, but I think it's not very efficient. Something tells me I'm going the plex-all-around way and there must be an easier path, but I can't get it:

 Object.keys(data)
                .map((elem, i) => {
                    return Object.assign({role: e}, Object.values(data)[i]);
                })

Is this the cleanest way to do what I intend to?

Thanks

I'm trying to find a more elegant way of converting a dictionary of dictionaries to an array of dictionaries and keeping the information of the keys of the original dictionary.

In the case of

let data= { boss: { name:"Peter", phone:"123"},
            minion: { name:"Bob", phone:"456"},
            slave: { name:"Pat", phone: "789"} 
          }

I want to e up with something that gives me

output = [ { role:"boss", name:"Peter", phone:"123"},
           { role:"minion", name:"Bob", phone:"456"},
           { role:"slave", name:"Pat",  phone:"789"}
         ]

My solution goes by using the Object.keys method, but I think it's not very efficient. Something tells me I'm going the plex-all-around way and there must be an easier path, but I can't get it:

 Object.keys(data)
                .map((elem, i) => {
                    return Object.assign({role: e}, Object.values(data)[i]);
                })

Is this the cleanest way to do what I intend to?

Thanks

Share Improve this question edited Oct 2, 2020 at 10:32 ggonmar asked Oct 2, 2020 at 8:12 ggonmarggonmar 8391 gold badge12 silver badges31 bronze badges 1
  • 1 You can use const output = Object.entries(data).map(({ role, value) => ({ ...value, role })); – user5734311 Commented Oct 2, 2020 at 8:16
Add a ment  | 

3 Answers 3

Reset to default 7

You could map the entries and pick the key/role for a new object.

let data = { boss: { name: "Peter", phone: "123" }, minion: { name: "Bob", phone: "456" }, slave: { name: "Pat", phone: "789" } },
    result = Object.entries(data).map(([role, values]) => ({ role, ...values }));

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

Using Object.entries, you can get [key, value] pairs as array and can get teh result you want using Array.map function.

let data = {
  boss: { name:"Peter", phone:"123"},
  minion: { name:"Bob", phone:"456"},
  slave: { name:"Pat", phone: "789"}
};

const result = Object.entries(data).map(([key, value]) => ({
  role: key,
  ...value
}));
console.log(result);

You can use Array.prototype.reduce and Object.entries to loop over every entry in the dictionary and reduce it to an array:

const data = { boss: { name: "Peter", phone: "123" }, minion: { name: "Bob", phone: "456" }, slave: { name: "Pat", phone: "789" } };

function getArray(data) {
  return Object.entries(data).reduce((a, [k, v]) => {
    a.push({role: k, ...v});
    return a;
  }, []);
}
console.log(getArray(data));

发布评论

评论列表(0)

  1. 暂无评论