嗨,我有一个像这样的对象数组
[{"$value":"Cardiology","$id":"0","$priority":null},
{"$value":"Dermatology","$id":"1","$priority":null},
{"$value":"Physiology","$id":"2","$priority":null},{"$value":"Child
Physiology","$id":"3","$priority":null}]
我想将其转换为
['Cardiology','Dermatology','Physiology','Child Physiology']
任何帮助将不胜感激。
答案 0 :(得分:1)
您需要遍历数组并创建另一个数组。
var arr = [{"$value":"Cardiology","$id":"0","$priority":null},
{"$value":"Dermatology","$id":"1","$priority":null},
{"$value":"Physiology","$id":"2","$priority":null},{"$value":"Child Physiology","$id":"3","$priority":null}];
var arr1 = [];
for (var i = 0 ; i < arr.length; i++) {
arr1.push(arr[i].$value);
}
console.log(arr1);
答案 1 :(得分:1)
您可以使用Array.map()
var array = [{
"$value": "Cardiology",
"$id": "0",
"$priority": null
}, {
"$value": "Dermatology",
"$id": "1",
"$priority": null
}, {
"$value": "Physiology",
"$id": "2",
"$priority": null
}, {
"$value": "Child Physiology",
"$id": "3",
"$priority": null
}]
var newarray = array.map(function(obj) {
return obj.$value
});
snippet.log(JSON.stringify(newarray))
&#13;
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<!-- To show result in the dom instead of console, only to be used in the snippet not in production -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
&#13;