Convert an object into a dictionary of object

14,585

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Share:
14,585

Related videos on Youtube

Laurent Maquet
Author by

Laurent Maquet

Updated on June 04, 2022

Comments

  • Laurent Maquet
    Laurent Maquet almost 2 years

    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...

    • Nina Scholz
      Nina Scholz about 6 years
      structuredObject is an array, but it has properties, which is not possible as literal. please add a valid data structure as result.
    • Jonas Wilms
      Jonas Wilms about 6 years
      Why?? Whats the reasoning for this?
    • Laurent Maquet
      Laurent Maquet about 6 years
      Yes, it is an array of structured objects, each having properties. I have edited my snippets to make it clear
    • Laurent Maquet
      Laurent Maquet about 6 years
      @jonas : the reasoning is to enrich each object with 'foo'
  • Laurent Maquet
    Laurent Maquet about 6 years
    understood ! I actually need a dictionnary of object... I have edited my question
  • Laurent Maquet
    Laurent Maquet about 6 years
    Thanks : this should sove my problem, but I get an error : Object.entries is not a function. I'm using Firebase Cloud Functions with Node v6.11.5
  • tiagodws
    tiagodws about 6 years
    @LaurentMaquet Edited my answer with what you wanted as well :)
  • Laurent Maquet
    Laurent Maquet about 6 years
    OK thanks, tiagodws, but I'm trying to implement your solution without a separate function. I tried this one but it seems it is not correct... names = Object.keys(obj || {}).reduce((result, key) => (result[key] = {name: obj[key], type: 'foo'}));
  • tiagodws
    tiagodws about 6 years
    @LaurentMaquet on your reduce function, you need to return the whole result object :) the way you did it ((result[key] = {name: obj[key], type: 'foo'})), it is only returning the current object. You could try that: names = Object.keys(obj || {}).reduce((result, key) => (result[key] = {name: obj[key], type: 'foo'}) && result, {});
  • Laurent Maquet
    Laurent Maquet about 6 years
    Thanks tiagodws. It's ok now !