我有一个像这样的对象作为服务器的JSON响应返回:
{"0":"1","1":"2","2":"3","3":"4"}
我想将其转换为如下所示的JavaScript数组:
["1","2","3","4"]
有没有最好的方法来做到这一点?无论我在哪里阅读,人们都在使用复杂的循环逻辑。那么有替代方法可以做到这一点吗?
我有一个像这样的对象作为服务器的JSON响应返回:
{"0":"1","1":"2","2":"3","3":"4"}
我想将其转换为如下所示的JavaScript数组:
["1","2","3","4"]
有没有最好的方法来做到这一点?无论我在哪里阅读,人们都在使用复杂的循环逻辑。那么有替代方法可以做到这一点吗?
Answers:
jQuery的使用实际上非常简单 $.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是一种字符串格式),在这种情况下,JSON.parse
也需要进行遍历。
在ES2015中Object.values
,可以轻松进行救援
var arr = Object.values(obj);
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]);
}
希望这就是你所追求的!
就像“ JSON对象”一样,JSON是一种序列化表示法。
如果您想将javascript对象转换为javascript数组,则可以编写自己的循环[不会那么复杂!],或者依靠underscore.js _.toArray()
方法:
var obj = {"0":"1","1":"2","2":"3","3":"4"};
var yourArray = _(obj).toArray();
这里没什么难的。循环遍历对象元素并将其分配给数组
var obj = {"0":"1","1":"2","2":"3","3":"4"};
var arr = [];
for (elem in obj) {
arr.push(obj[elem]);
}
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];
}
}
console.log(array)
不知道我在这里缺少什么,只是尝试下面的代码即可。我在这里想念什么吗?
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));
}
})
假设您具有如下所示的值
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转换为多维JavaScript数组。
当使用php json编码的字符串时,此页面上的其他方法似乎对我来说都不完全起作用,除了我在这里提到的方法。
var data = [];
data = {{ jdata|safe }}; //parse through js
var i = 0 ;
for (i=0;i<data.length;i++){
data[i] = data[i].value;
}
$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++;
}
您可以使用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
数组对象。