我有一个这样的对象作为来自服务器的JSON响应返回:
{"0":"1","1":"2","2":"3","3":"4"}
我想将它转换为像这样的JavaScript数组:
["1","2","3","4"]
有最佳方法吗?无论我在哪里阅读,人们都在使用循环来使用复杂的逻辑。那么有没有其他方法可以做到这一点?
答案 0 :(得分:325)
jQuery' s $.map
var arr = $.map(obj, function(el) { return el });
并且在没有jQuery的情况下也很容易,将键转换为数组,然后使用Array.map
var arr = Object.keys(obj).map(function(k) { return obj[k] });
假设它已经被解析为javascript对象,并且实际上并不是JSON,这是一种字符串格式,在这种情况下,需要运行JSON.parse
同样。
在ES2015中有Object.values
来救援,这让这变得轻而易举
var arr = Object.values(obj);
答案 1 :(得分:105)
var json = '{"0":"1","1":"2","2":"3","3":"4"}';
var parsed = JSON.parse(json);
var arr = [];
for(var x in parsed){
arr.push(parsed[x]);
}
希望这就是你所追求的!
答案 2 :(得分:24)
你只需要像
一样var data = {
"0": "1",
"1": "2",
"2": "3",
"3": "4"
};
var arr = [];
for (var prop in data) {
arr.push(data[prop]);
}
console.log(arr);
答案 3 :(得分:20)
没有什么比“JSON对象” - JSON是序列化表示法。
如果你想将 javascript对象转换为 javascript数组,你要么编写自己的循环[这不会那么复杂!],或者你依赖于underscore.js _.toArray()
方法:
var obj = {"0":"1","1":"2","2":"3","3":"4"};
var yourArray = _(obj).toArray();
答案 4 :(得分:9)
这里没什么难的。循环遍历对象元素并将它们分配给数组
var obj = {"0":"1","1":"2","2":"3","3":"4"};
var arr = [];
for (elem in obj) {
arr.push(obj[elem]);
}
答案 5 :(得分:8)
var JsonObj= {"0":"1","1":"2","2":"3","3":"4"};
var array = [];
for(var i in JsonObj) {
if(JsonObj.hasOwnProperty(i) && !isNaN(+i)) {
array[+i] = JsonObj[i];
}
}
答案 6 :(得分:4)
试试这个:
var newArr = [];
$.each(JSONObject.results.bindings, function(i, obj) {
newArr.push([obj.value]);
});
答案 7 :(得分:2)
使用原始javascript,假设你有:
var j = {0: "1", 1: "2", 2: "3", 3: "4"};
您可以使用以下内容获取值:
Object.keys(j).map(function(_) { return j[_]; })
输出:
["1", "2", "3", "4"]
答案 8 :(得分:2)
不确定我在这里缺少什么,但只是尝试下面的代码就可以了。我在这里错过了什么吗?
https://jsfiddle.net/vatsalpande/w3ew5bhq/
$(document).ready(function(){
var json = {
"code" :"1",
"data" : {
"0" : {"id":"1","score":"44"},
"1" : {"id":"1","score":"44"}
}
};
createUpdatedJson();
function createUpdatedJson(){
var updatedJson = json;
updatedJson.data = [updatedJson.data];
$('#jsondata').html(JSON.stringify(updatedJson));
console.log(JSON.stringify(updatedJson));
}
})
答案 9 :(得分:2)
var obj = {"0":"1","1":"2","2":"3","3":"4"};
var vals = Object.values(obj);
console.log(vals); //["1", "2", "3", "4"]
问题的另一种选择
var vals = Object.values(JSON.parse(obj)); //where json needs to be parsed
答案 10 :(得分:1)
假设您有类似以下的值
var obj = {"0":"1","1":"2","2":"3","3":"4"};
然后您可以使用以下
将其转换为javascript数组var arr = [];
json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
arr = $.parseJSON(json); //convert to javascript array
这适用于将json转换为多diminsional javascript数组。
除了我在这里提到的方法之外,使用php json编码的字符串时,此页面上的其他任何方法似乎都不适合我。
答案 11 :(得分:1)
以下是如何获取对象数组 和 然后对数组进行排序的示例。
function osort(obj)
{ // map the object to an array [key, obj[key]]
return Object.keys(obj).map(function(key) { return [key, obj[key]] }).sort(
function (keya, keyb)
{ // sort(from largest to smallest)
return keyb[1] - keya[1];
}
);
}
答案 12 :(得分:1)
这是最好的解决方案。我想是的。
Object.keys(obj).map(function(k){return {key: k, value: obj[k]}})
答案 13 :(得分:1)
$data='{"resultList":[{"id":"1839","displayName":"Analytics","subLine":""},{"id":"1015","displayName":"Automation","subLine":""},{"id":"1084","displayName":"Aviation","subLine":""},{"id":"554","displayName":"Apparel","subLine":""},{"id":"875","displayName":"Aerospace","subLine":""},{"id":"1990","displayName":"Account Reconciliation","subLine":""},{"id":"3657","displayName":"Android","subLine":""},{"id":"1262","displayName":"Apache","subLine":""},{"id":"1440","displayName":"Acting","subLine":""},{"id":"710","displayName":"Aircraft","subLine":""},{"id":"12187","displayName":"AAC","subLine":""}, {"id":"20365","displayName":"AAT","subLine":""}, {"id":"7849","displayName":"AAP","subLine":""}, {"id":"20511","displayName":"AACR2","subLine":""}, {"id":"28585","displayName":"AASHTO","subLine":""}, {"id":"45191","displayName":"AAMS","subLine":""}]}';
$b=json_decode($data);
$i=0;
while($b->{'resultList'}[$i])
{
print_r($b->{'resultList'}[$i]->{'displayName'});
echo "<br />";
$i++;
}
答案 14 :(得分:0)
var data = [];
data = {{ jdata|safe }}; //parse through js
var i = 0 ;
for (i=0;i<data.length;i++){
data[i] = data[i].value;
}
答案 15 :(得分:0)
您可以将Object.assign()
与空数组文字[]
用作target
:
const input = {
"0": "1",
"1": "2",
"2": "3",
"3": "4"
}
const output = Object.assign([], input)
console.log(output)
如果您选中polyfill,Object.assign(target, ...sources)
只是将所有可枚举的属性从source
对象复制到目标对象。如果target
是一个数组,它将数字键添加到数组文字中并返回该target
数组对象。