Make object key a value in the equivalent sub-object

不羁岁月 提交于 2019-12-23 04:44:07

问题


I have a complex object where the key of each object property should actually be a value. So, I am trying to convert this:

{
  "2019-04-24T00:00:00Z": {
    "one": 185,
  },
  "2019-04-25T00:00:00Z": {
    "one": 207,
  }
}

to this:

{
  {
    "date": "2019-04-24T00:00:00Z",
    "one": 185,
  },
  {
    "date": "2019-04-25T00:00:00Z",
    "one": 207,
  }
}

I have pasted a snippet with an attempt.

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics.");

var found = Object.keys(json2).find(function(layerKey) {
  console.log(json2[layerKey]);
});

for (var i = 0; i<= Object.keys(json2).length; i++) {
  for (var prop in json2[i]) {
    console.log("Key:" + prop);
    //Add date as a value in the JSON
    //json2[i].map(i=>i.Date=prop);
    json2[i]["Date"] = prop;
  }
}

//json2.map(i=>i.Date=prop);
console.log(json2);
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>

回答1:


You can try using nested map

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics");

var result = json2.map(e =>
  Object.keys(e).map(m => Object.assign(e[m], {
    data: m
  }))
)

console.log(result)
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>



回答2:


You may want an array as output, because the output you provide is not a valid js object literal.

const obj = {"2019-04-24T00:00:00Z": {"one": 185,},"2019-04-25T00:00:00Z": {"one": 207,}};

const newArr = Object.keys(obj).map(item=>({...obj[item],date:item}));


console.log(newArr);



回答3:


Try this:

const a = {
  "2019-04-24T00:00:00Z": {
    "one": 185,
  },
  "2019-04-25T00:00:00Z": {
    "one": 207,
  }
}

const result = Object.keys(a).reduce((acc, ele)=> acc =  [...acc,{date: ele, one: a[ele].one}] ,[]);

console.log(result);



回答4:


There you go:

var example = {"2019-04-24T00:00:00Z": {"one": 185}, "2019-04-25T00:00:00Z": {"one": 207}}

var result = []
for (var key in example) {
    var objectCopy = JSON.parse(JSON.stringify(example[key]));
    objectCopy['date'] = key;
    result.push(objectCopy); 
}
console.log(result)
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>



回答5:


To me, Object.entries() is the most natural way to achieve your goal:

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics.");

const res = json2.map(item => Object.entries(item).map(([date, obj]) => ({...obj,date})));

console.log(res);
.as-console-wrapper {min-height: 100%}
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>


来源:https://stackoverflow.com/questions/56775220/make-object-key-a-value-in-the-equivalent-sub-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!