JSON键作为javascript中的JSON值

时间:2022-04-28 04:49:02

Someone knows how I can to convert this JSON:

有人知道如何转换这个JSON:

{"MASCULINO": 10, "FEMENINO": 20, "NO GENERO": 70}

To this JSON:

对于这个JSON:

"MASCULINO": {
    "MASCULINO": 10
},
"FEMENINO": {
    "FEMENINO": 20
},
"NO GÉNERO": {
    "NO GÉNERO": 70
}

I do this because I have this error XLSX-CHART cannot graphic simple JSON and this is the only solution that I found...

我这样做是因为我有这个错误XLSX-CHART无法绘制简单的JSON图形,这是我找到的唯一解决方案...

Thank you :)

谢谢 :)

2 个解决方案

#1


2  

You can use reduce for this:

你可以使用reduce来:

const obj = {"MASCULINO": 10, "FEMENINO": 20, "NO GENERO": 70};

const result = Object.keys(obj).reduce((acc, curr) => ({
  ...acc,
  [curr]: {
    [curr]: obj[curr],
  }
}), {});

console.log(result);

#2


0  

use lodash lib

使用lodash lib

const obj = {"MASCULINO": 10, "FEMENINO": 20, "NO GENERO": 70};
const newObj = _.transform(obj, function(result, value, key) {
     return result[key] = { [key]: value };
}, {});
console.log(newObj);

#1


2  

You can use reduce for this:

你可以使用reduce来:

const obj = {"MASCULINO": 10, "FEMENINO": 20, "NO GENERO": 70};

const result = Object.keys(obj).reduce((acc, curr) => ({
  ...acc,
  [curr]: {
    [curr]: obj[curr],
  }
}), {});

console.log(result);

#2


0  

use lodash lib

使用lodash lib

const obj = {"MASCULINO": 10, "FEMENINO": 20, "NO GENERO": 70};
const newObj = _.transform(obj, function(result, value, key) {
     return result[key] = { [key]: value };
}, {});
console.log(newObj);