簡體   English   中英

將數組[object,object…]轉換為json

[英]Transform array [object, object …] to json

這里我有一個帶有對象的數組:(數組可以有N個對象...)

[Object, Object]
  0: Object
    0: "2"
    1: "Laza Lazic"
    id_tabele: "2"
    naziv: "Laza Lazic"
__proto__: Object
 1: Object
    0: "1"
    1: "Pera Peric"
    id_tabele: "1"
    naziv: "Pera Peric"
__proto__: Object
length: 2
__proto__: Array[0]

如何使用javascript(jquery)將對象數組轉換為這種格式:

[{"id":"1","ime_prezime":"Pera Peric"},{"id":"2","ime_prezime":"Laza Lazic"}]

所以id_tabele從數組對象是id在JSON和nazivime_prezime在JSON ...

使用$ .map()

var array = $.map(oldarray, function (obj) {
    return {
        id: obj.id_tabele,
        naziv: obj.ime_prezime
    };
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM