将Javascript值提高一级-使key:object:key只是object:key

时间:2018-09-19 12:52:52

标签: javascript

我有以下javascript对象:

{"475":{"proposed_submission_date":"20/Sep/2018","pk":"475"},
 "512":{"proposed_submission_date":"20/Sep/2018","pk":"512"}}

我想删除'475'和'512'键,以便输出为:

{
     {"proposed_submission_date":"20/Sep/2018","pk":"475"},
     {"proposed_submission_date":"20/Sep/2018","pk":"512"}
}

使用JavaScript可以吗?

我尝试了几种方法,但无法使其正常工作。

3 个答案:

答案 0 :(得分:2)

obj = Object.keys(your_JSON_OBJ).map((key)=>{
  return your_JSON_OBJ[key]
}

这将返回一个子对象数组。您将无法将它们移动到具有数组结构的对象中,因为您的子元素之外现在没有“ key:value”的“ key”部分。

答案 1 :(得分:2)

如果您要查找注释中建议的对象数组,则可以使用Object.values()

var obj = {
  "475":{"proposed_submission_date":"20/Sep/2018","pk":"475"},
  "512":{"proposed_submission_date":"20/Sep/2018","pk":"512"}
}

console.log(Object.values(obj))

答案 2 :(得分:1)

<script>

let a = {"475":{"proposed_submission_date":"20/Sep/2018","pk":"475"},
 "512":{"proposed_submission_date":"20/Sep/2018","pk":"512"}};

 console.log(a);

let myArray = [];  
for (var property in a) {
    myArray.push( a[property]);
}

 console.log(myArray);


</script>