使对象键成为等效子对象

时间:2019-06-26 14:31:13

标签: javascript jsonpath

我有一个复杂的对象,其中每个对象属性的键实际上应该是一个值。因此,我正在尝试将其转换为

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

对此:

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

我尝试粘贴了一段代码。

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>

5 个答案:

答案 0 :(得分:2)

您可以尝试使用嵌套的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>

答案 1 :(得分:2)

您可能希望将数组作为输出,因为您提供的输出不是有效的js对象文字。

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

答案 2 :(得分:1)

尝试一下:

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

答案 3 :(得分:1)

去那里:

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>

答案 4 :(得分:1)

对我来说,Object.entries()是实现目标的最自然的方法:

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 {
  max-height: 100% !important;
  top: 0;
}
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>