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

javascript - Convert an object into a dictionary of object - Stack Overflow

programmeradmin4浏览0评论

I have an object with unknown key/values pairs like that

myObj = {
    key_1: value_1,
    key_2: value_2,
    key_n: value_n
}

I would like to convert it into a dictionary of structured objects like this one:

dictOfStructureObjects =
{
 key_1: {
  name: value_1,
  type: 'foo'},
 key_2: {
  name: value_2,
  type: 'foo'},
 key_n: {
  name: value_n,
  type: 'foo'}
}

I tried this one :

var dic = snapshot.val();
var arr = Object.keys(dic || {});
var names = arr.map(element => {
    var rObj = {};
    rObj[element.value] = {name: dic[element.value], type: 'foo'};
});

But I think that trying to refer to the value of an array element with its property value is not correct...

I have an object with unknown key/values pairs like that

myObj = {
    key_1: value_1,
    key_2: value_2,
    key_n: value_n
}

I would like to convert it into a dictionary of structured objects like this one:

dictOfStructureObjects =
{
 key_1: {
  name: value_1,
  type: 'foo'},
 key_2: {
  name: value_2,
  type: 'foo'},
 key_n: {
  name: value_n,
  type: 'foo'}
}

I tried this one :

var dic = snapshot.val();
var arr = Object.keys(dic || {});
var names = arr.map(element => {
    var rObj = {};
    rObj[element.value] = {name: dic[element.value], type: 'foo'};
});

But I think that trying to refer to the value of an array element with its property value is not correct...

Share Improve this question edited Jul 16, 2022 at 3:06 Spectric 32k6 gold badges28 silver badges51 bronze badges asked Feb 5, 2018 at 15:20 Laurent MaquetLaurent Maquet 4001 gold badge4 silver badges15 bronze badges 4
  • 5 structuredObject is an array, but it has properties, which is not possible as literal. please add a valid data structure as result. – Nina Scholz Commented Feb 5, 2018 at 15:22
  • Why?? Whats the reasoning for this? – Jonas Wilms Commented Feb 5, 2018 at 15:26
  • Yes, it is an array of structured objects, each having properties. I have edited my snippets to make it clear – Laurent Maquet Commented Feb 5, 2018 at 15:28
  • @jonas : the reasoning is to enrich each object with 'foo' – Laurent Maquet Commented Feb 5, 2018 at 15:29
Add a comment  | 

4 Answers 4

Reset to default 10

As pointed out in the comments, your expected output isn't a valid array.

If you want a valid array, you could do it like that:

function convert(obj) {
    return Object.keys(obj).map(key => ({
        name: key,
        value: obj[key],
        type: "foo"
    }));
}

const myObj = {
    key_1: "value_1",
    key_2: "value_2",
    key_3: "value_3",
};

console.log(convert(myObj));

And if you want it as an object, like that:

function convert(obj) {
    return Object.keys(obj).reduce((result, key) => {
        result[key] = {
            name: obj[key],
            type: "foo"
        };
        return result;
    }, {});
}

const myObj = {
    key_1: "value_1",
    key_2: "value_2",
    key_3: "value_3",
};

console.log(convert(myObj));

You could map the keys as new object with the wanted key and new object.

var object = { key_1: 'value_1', key_2: 'value_2', key_n: 'value_n' },
    newObject = Object.assign(
        ...Object.keys(object).map(k => ({ [k]: { name: object[k], type: 'foo' } }))
    );
    
console.log(newObject);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you want an array...

myObj = {
  key_1: "value1",
  key_2: "value_2",
  key_n: "value_n"
}

var a = [];
Object.entries(myObj).forEach(itm=>a.push({key: itm[0], value: itm[1]}));
console.log(a);

If you want an object

myObj = {
  key_1: "value1",
  key_2: "value_2",
  key_n: "value_n"
}

var a = {};
Object.entries(myObj).forEach(itm=>a[itm[0]] = {key: itm[0], value: itm[1]});
console.log(a);

Title is confusing. The op doesn't actually want to convert from a class object to a dictionary. He wants to modify a datastructure.

If you actually want to convert from a class object to a dictionary:

JSON.parse(JSON.stringify(obj))
发布评论

评论列表(0)

  1. 暂无评论